Qt drag operation is basically divided into three parts: a data acquisition (QMimeData) required for recording, the drag operation (the Drag), drop operation (the Drop);
QMimeData: multimedia data type, drag and drop events to pass data through the object support: text, html data, legal path, pictures, colors and so on;
Drag: drag and drop user an intuitive way to copy or move data in the program, which handles most of the details of the drag and drop operation
QDropEvent: providing a trigger event when the drag operation is completed
Drag and drop a picture as an example:
void Widget :: mousePressEvent (QMouseEvent * e) // mouse click events
{
QLabel * child = static_cast <QLabel *> (childAt (e-> pos ())); // Gets the location of the mouse click
if(!child)
{
return;
}
QPixmap pixmap = * child-> pixmap (); // get the picture information on the control
QByteArray itemData;
QDataStream dataStream(&itemData,QIODevice::WriteOnly);
dataStream<<pixmap<<QPoint(e->pos()-child->pos());
QMimeData * mimeData = new QMimeData; // record data required
mimeData->setData("appDrop",itemData);
QDrag * drag = new QDrag (this); // create a drag and drop operation
drag-> setMimeData (mimeData); // associated data, when the drag and drop process on the data tape
pixmap=pixmap.scaled(pixmap.width()*0.1,pixmap.height()*0.1);
drag-> setPixmap (pixmap); // set when dragging the mouse to display the picture in
drag-> setHotSpot (e-> pos ()); // position, can see the note under the effect of
QPixmap tempPixmap=pixmap;
QPainter painter;
painter.begin(&tempPixmap);
drag-> exec (Qt :: CopyAction | Qt :: MoveAction, Qt :: CopyAction); // event that triggers a drag, you can start dragging
}
Local content will be placed onto / over the control, release the mouse to trigger the function
void Widget::dropEvent(QDropEvent *e)
{
if (e-> mimeData () -> hasFormat ( "appDrop")) // carrying the drag operation determines whether there is data
{Will carry out data analysis, parsing information into the original image, text, location, etc.
QByteArray itemData=e->mimeData()->data("appDrop");
QDataStream dataStream(&itemData,QIODevice::ReadOnly);
QPixmap pixmap;
QPoint offset;
dataStream>>pixmap>>offset;
// display the data placement operation
ui->label_2->setPixmap(pixmap);//前提setAcceptDrops(true);
}
}
// mouse to drag and drop into the target control
void Widget :: wear delicate guy (QDragEnterEvent * e)
{
if(e->mimeData()->hasFormat("appDrop"))
{
if(e->source()==this)
{
e->setDropAction(Qt::MoveAction);
e->accept();
}
else{
e->acceptProposedAction();
}
}else
{
e->ignore();
}
}
setAcceptDrops (bool); // set the parts down whether to accept event (drop),
event-> acceptProposedAction () // drag and drop operation to the recommended operation and acceptance of the event
event-> ignore () // ignore the data, for processing data unavailable
enum DropAction {
CopyAction = 0x1 , // copy the data to the destination
MoveAction = 0x2 , // move data from source to target
LinkAction = 0x4 , // create a link from source to destination
ActionMask = 0xff , // no explanation
TargetMoveAction = 0x8002 ,//
IgnoreAction = 0x0 // skip the operation (data not processed)
};
Published 104 original articles
·
won praise 22
·
views 40000 +