PDA

View Full Version : Problems with multiple selection in Drag & Drop



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
}
}

anda_skoa
15th December 2015, 10:00
If you want to drag the stylesheet information of a widget then that's a way of adding that to QMimeData.
The setStyleSheet() would of course be done at the drop location.

Cheers,
_

JonSim
15th December 2015, 11:16
I like to drag all the QLabel found in my GUI around. There are QImage applied to the QLabel. But i am unable to drag it as a while(multiple selection for all the QLabel)

anda_skoa
15th December 2015, 11:52
I am afraid I don't understand.

If you want to drag labels, why create a data drag and not just move the labels?

Cheers,
_