JonSim
15th December 2015, 03:25
Hi Guys,
I have encountered problem in dragging and dropping multiple QLabel and QWidget at the same time (multiple selection of QLabel & QWidget) which I have used in my application. In my application, there are images and texts needed to be drag & drop together.
Can someone please help in solving this problem? Written below are the codes I have written in my MainWindow.cpp file.
Thank You
void MainWindow::mousePressEvent(QMouseEvent *event)
{
QWidget *child = static_cast<QWidget *>(childAt(event->pos()));
if (!child)
return;
QPoint hotSpot = event->pos() - child->pos();
QString textdata = child->styleSheet(); //not sure if this syntax is correct
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << textdata << hotSpot;
QMimeData *mimeData = new QMimeData;
mimeData->setData("mydatatype", itemData);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(textdata);
drag->setHotSpot(hotSpot);
if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
{
child->close();
}
else
{
child->show();
child->setStyleSheet(textdata); //not sure if this syntax is correct
}
}
I have encountered problem in dragging and dropping multiple QLabel and QWidget at the same time (multiple selection of QLabel & QWidget) which I have used in my application. In my application, there are images and texts needed to be drag & drop together.
Can someone please help in solving this problem? Written below are the codes I have written in my MainWindow.cpp file.
Thank You
void MainWindow::mousePressEvent(QMouseEvent *event)
{
QWidget *child = static_cast<QWidget *>(childAt(event->pos()));
if (!child)
return;
QPoint hotSpot = event->pos() - child->pos();
QString textdata = child->styleSheet(); //not sure if this syntax is correct
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << textdata << hotSpot;
QMimeData *mimeData = new QMimeData;
mimeData->setData("mydatatype", itemData);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(textdata);
drag->setHotSpot(hotSpot);
if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
{
child->close();
}
else
{
child->show();
child->setStyleSheet(textdata); //not sure if this syntax is correct
}
}