Hi,
I am dragging a QTreeWidgetItem from one QTreeWidget to another. How can I draw a pixmap with the text (caption) of the dragged item to use it in QDrag?
Thanks,
Carlos.
Printable View
Hi,
I am dragging a QTreeWidgetItem from one QTreeWidget to another. How can I draw a pixmap with the text (caption) of the dragged item to use it in QDrag?
Thanks,
Carlos.
I use this code...
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
mimeData->setText("Hello World");
drag->setMimeData(mimeData);
drag->setPixmap(yourPixmap);
Qt::DropAction dropAction = drag->start();
Hi,
I have almost the same code but I want to show which QTreeWidgetItem is dragging hence the pixmap must be created with the caption of the QTreeWidgetItem!
Dennis, you indicatedbut how you create yourPixmap with the caption of the QTreeWidgetItem?Code:
drag->setPixmap(yourPixmap)
Carlos.
Can't you use QPainter to draw text on the pixmap, before you pass it to QDrag ?
I tried that with:
Code:
But I still don't get it!
Well, you are trying to draw on a null pixmap...
Initialize the pixmap with non-zero size, fill it with background color, then draw text.
ok.
Almost there, now I get a white pixmap with the appropriate size but without the text.
Code:
pixmap.fill(); paint.drawText(met.boundingRect("Some text here"),Qt::AlignLeft,"Some text here");
I reckon painter is not starting to draw in a good position.
Yep! It was the Y... it must be negative
Code:
paint.drawText(0,0-rect.y(),"Some text here");
Thanks for your help.
When you create the QTreeWidgetItem, do not set the text or icon.
Instead, create a QWidget that contains 2 QLabels (one for the icon, one for the text), then use the QTreeWidget::setItemWidget method to associate the QWidget with the QTreeWidgetItem.
When you see the mousePressEvent that starts the drag, get the QTreeWidgetItem *drag_item that's under the mouse by calling QTreeWidget::itemAt(event->pos()).
When creating the QDrag, create the pixmap by doing something like this:
QWidget *w = itemWidget(drag_item,0);
QPixmap pixmap(w->size());
w->render(&pixmap);
QDrag *drag = new QDrag;
drag->setPixmap(pixmap);