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:
///
///
///
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);
}
}
}
///
///
///
void SourceTreeWidget::mousePressEvent(QMouseEvent *event)
{
if ( event->button() == Qt::LeftButton )
{
QTreeWidgetItem * item = itemAt(event->pos());
if ( item && item->childCount() == 0 ) {
startPos = event->pos();
}
else {
startPos.setX(0); startPos.setY(0);
}
}
QTreeWidget::mousePressEvent(event);
}
To copy to clipboard, switch view to plain text mode
///
///
///
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);
}
}
}
///
///
///
void SourceTreeWidget::mouseMoveEvent(QMouseEvent *event)
{
if ( (event->buttons() & Qt::LeftButton) && !startPos.isNull() )
{
int distance = (event->pos() - startPos).manhattanLength();
if (distance >= QApplication::startDragDistance())
{
QTreeWidgetItem * dragTreeViewItem = itemAt(startPos);
//
// Paint an image composed of a transparent background and an icon and some text
//
QImage icon(QString(":/images/bean.ico"));
QSize dragImageSize(186, 20);
QImage dragImage(dragImageSize, QImage::Format_ARGB32_Premultiplied);
QPainter dragPainter(&dragImage);
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();
QDrag *drag = new QDrag(this);
drag->setPixmap( QPixmap::fromImage(dragImage) );
QMimeData *mimeData = new QMimeData;
mimeData->setText(dragTreeViewItem->text(0));
drag->setMimeData(mimeData);
drag->exec(Qt::MoveAction);
}
}
QTreeWidget::mouseMoveEvent(event);
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks