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.
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:ropAction 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?Qt Code:
drag->setPixmap(yourPixmap)To copy to clipboard, switch view to plain text mode
Carlos.
Can't you use QPainter to draw text on the pixmap, before you pass it to QDrag ?
I tried that with:
Qt Code:To copy to clipboard, switch view to plain text mode
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.
qlands (27th September 2011)
ok.
Almost there, now I get a white pixmap with the appropriate size but without the text.
Qt Code:
pixmap.fill(); paint.drawText(met.boundingRect("Some text here"),Qt::AlignLeft,"Some text here");To copy to clipboard, switch view to plain text mode
I reckon painter is not starting to draw in a good position.
Yep! It was the Y... it must be negative
Qt Code:
paint.drawText(0,0-rect.y(),"Some text here");To copy to clipboard, switch view to plain text mode
Thanks for your help.
Last edited by qlands; 27th September 2011 at 12:37.
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);
Bookmarks