Hello! Im dragging from a QTreeWidget to QGraphicsScene. And drop action doesnt occur.
This code for drag:
{
if (event->button() == Qt::LeftButton)
dragStartPosition = event->pos();
}
{
if (!(event->buttons() & Qt::LeftButton))
return;
if ((event
->pos
() - dragStartPosition
).
manhattanLength() <
QApplication::startDragDistance()) return;
if(this
->currentItem
()->text
(0) == QString::fromLocal8Bit("Text")) {
lb->setMaximumSize(100,20);
lb->setAlignment(Qt::AlignVCenter|Qt::AlignHCenter);
drag
->setPixmap
(QPixmap::grabWidget(lb
));
drag->setMimeData(mimeData);
Qt::DropAction dropAction = drag->exec();
}
void MyTreeWidget::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
dragStartPosition = event->pos();
QTreeWidget::mousePressEvent(event);
}
void MyTreeWidget::mouseMoveEvent(QMouseEvent* event)
{
if (!(event->buttons() & Qt::LeftButton))
return;
if ((event->pos() - dragStartPosition).manhattanLength() < QApplication::startDragDistance())
return;
if(this->currentItem()->text(0) == QString::fromLocal8Bit("Text"))
{
QLabel* lb = new QLabel(QString::fromLocal8Bit("Text"));
lb->setMaximumSize(100,20);
lb->setAlignment(Qt::AlignVCenter|Qt::AlignHCenter);
QDrag *drag = new QDrag(this);
drag->setPixmap(QPixmap::grabWidget(lb));
QMimeData *mimeData = new QMimeData;
drag->setMimeData(mimeData);
Qt::DropAction dropAction = drag->exec();
}
To copy to clipboard, switch view to plain text mode
And this is for drop:
{
setAcceptDrops(true);
}
{
cout << "drag entered" << endl;
e->acceptProposedAction();
}
{
cout << "dropped" << endl;
e->acceptProposedAction();
}
MyGraphicsView::MyGraphicsView(QGraphicsScene* parent) : QGraphicsView(parent)
{
setAcceptDrops(true);
}
void MyGraphicsView::dragEnterEvent(QDragEnterEvent* e)
{
cout << "drag entered" << endl;
e->acceptProposedAction();
}
void MyGraphicsView::dropEvent(QDropEvent *e)
{
cout << "dropped" << endl;
e->acceptProposedAction();
}
To copy to clipboard, switch view to plain text mode
dragEnterEvent works correctly (at least cout in it). But dropEvent doesnt. And drop accepted symbol (+) isnt shown over pixmap im dragging.
Help would be appreciated. Thanks in advance.
Bookmarks