Re: QTreeWidget + drag&drop : How to set pixmap as Icon+Text
Hi,
During a drag&drop I would like to display the icon and the text of the tree-item in the drag element. How can I do this ?
If I set the pixmap, it only display the icon... but not the text !
Thanks
Added after 28 minutes:
I have a partial solution, like this
Code:
void QShaderNavigator
::mousePressEvent(QMouseEvent *event
) {
//---- Search for the selected definition
_dragTreeViewItem = itemAt(event->pos());
if (_dragTreeViewItem)
{
QVariant data
= _dragTreeViewItem
->data
(0,
1);
if (data.isNull())
_dragTreeViewItem = 0;
else
{
_dragPainter->drawPixmap(0, 0, 16, 16, image);
_dragPainter
->drawText
(QRect(20,
0,
170,
20), _dragTreeViewItem
->text
(0) );
}
}
}
Code:
void QShaderNavigator
::mouseMoveEvent(QMouseEvent * event
) {
...
drag->setPixmap( *_dragPixmap );
...
}
But the problem is that I have a big gray background in the Drag item !!!
1 Attachment(s)
Re: QTreeWidget + drag&drop : How to set pixmap as Icon+Text
This post is pretty old, but I'll write this up anyway.
You were on the right track, you just need to paint a transparent rectangle and then use the SourceOver compostion mode of the painter to draw your icon and text over top of the transparent background. The following code differs slightly from yours, but I think you will understand what I'm doing:
Code:
///
///
///
void SourceTreeWidget
::mousePressEvent(QMouseEvent *event
) {
if ( event->button() == Qt::LeftButton )
{
if ( item && item->childCount() == 0 ) {
startPos = event->pos();
}
else {
startPos.setX(0); startPos.setY(0);
}
}
}
Code:
///
///
///
void SourceTreeWidget
::mouseMoveEvent(QMouseEvent *event
) {
if ( (event->buttons() & Qt::LeftButton) && !startPos.isNull() )
{
int distance = (event->pos() - startPos).manhattanLength();
{
//
// Paint an image composed of a transparent background and an icon and some text
//
QSize dragImageSize
(186,
20);
QImage dragImage
(dragImageSize,
QImage::Format_ARGB32_Premultiplied);
dragPainter.
setCompositionMode(QPainter::CompositionMode_Source);
dragPainter.fillRect(dragImage.rect(), Qt::transparent);
dragPainter.
setCompositionMode(QPainter::CompositionMode_SourceOver);
dragPainter.
drawPixmap(0,
0,
16,
16,
QPixmap::fromImage(icon
));
dragPainter.
drawText(QRect(20,
0,
170,
20), dragTreeViewItem
->text
(0) );
dragPainter.end();
drag
->setPixmap
( QPixmap::fromImage(dragImage
) );
mimeData->setText(dragTreeViewItem->text(0));
drag->setMimeData(mimeData);
drag->exec(Qt::MoveAction);
}
}
}
I had to do the same thing for a project, but with multi-select. I'm attaching a project (treeMultiDrag.zip) that shows how to do the same thing with more elaborate multi-select.