PDA

View Full Version : Drop not working correctly



rippa
11th November 2008, 12:49
Hello! Im dragging from a QTreeWidget to QGraphicsScene. And drop action doesnt occur.

This code for drag:


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();
}

And this is for drop:


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();
}


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.

onamatic
11th November 2008, 15:51
I'm no expert and I've only used QTreeViews, not QTreeWidgets directly. It struck me that you don't set up any MimeFormats (or data) so the view won't know what's being dropped on it:



...
QMimeData *mimeData = new QMimeData;
//without the following line, my drag/drops with a QTreeView went badly wrong
mimeData->setData(("application/x-something", QByteArray()); //replace x-something and QByteArray() if necessary
drag->setMimeData(mimeData);
...

Just my ignorant thoughts - sorry if they're irrelevant!

rippa
11th November 2008, 16:13
Unfortunately, its not the point (. QMimeData instance should be specified fo QDrag to work, but i believe its not important is there any real information in this instance.
Besides im passing some real info in my programm. I just cut those blocks from example code for the sake of simplicity.

The problem is not that Drop drops something wrong, but that it doesnt even occur, like my accepting widget acceptDrops is FALSE.