Results 1 to 2 of 2

Thread: QTreeWidget + drag&drop : How to set pixmap as Icon+Text

  1. #1
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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

    Qt Code:
    1. void QShaderNavigator::mousePressEvent(QMouseEvent *event)
    2. {
    3. //---- Search for the selected definition
    4. _dragTreeViewItem = itemAt(event->pos());
    5. if (_dragTreeViewItem)
    6. {
    7. QVariant data = _dragTreeViewItem->data(0, 1);
    8. if (data.isNull())
    9. _dragTreeViewItem = 0;
    10. else
    11. {
    12. const QPixmap image = iconShader.pixmap(QSize(16,16));
    13. _dragPainter->drawPixmap(0, 0, 16, 16, image);
    14. _dragPainter->drawText(QRect(20, 0, 170, 20), _dragTreeViewItem->text(0) );
    15. }
    16. }
    17.  
    18. QTreeWidget::mousePressEvent(event);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void QShaderNavigator::mouseMoveEvent(QMouseEvent * event)
    2. {
    3. ...
    4. drag->setPixmap( *_dragPixmap );
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 

    But the problem is that I have a big gray background in the Drag item !!!
    Last edited by pl01; 5th April 2011 at 16:14.

  2. #2
    Join Date
    May 2011
    Location
    Ann Arbor, MI USA
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. ///
    2. ///
    3. ///
    4. void SourceTreeWidget::mousePressEvent(QMouseEvent *event)
    5. {
    6. if ( event->button() == Qt::LeftButton )
    7. {
    8. QTreeWidgetItem * item = itemAt(event->pos());
    9. if ( item && item->childCount() == 0 ) {
    10. startPos = event->pos();
    11. }
    12. else {
    13. startPos.setX(0); startPos.setY(0);
    14. }
    15. }
    16.  
    17. QTreeWidget::mousePressEvent(event);
    18. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ///
    2. ///
    3. ///
    4. void SourceTreeWidget::mouseMoveEvent(QMouseEvent *event)
    5. {
    6. if ( (event->buttons() & Qt::LeftButton) && !startPos.isNull() )
    7. {
    8. int distance = (event->pos() - startPos).manhattanLength();
    9. if (distance >= QApplication::startDragDistance())
    10. {
    11. QTreeWidgetItem * dragTreeViewItem = itemAt(startPos);
    12.  
    13. //
    14. // Paint an image composed of a transparent background and an icon and some text
    15. //
    16. QImage icon(QString(":/images/bean.ico"));
    17. QSize dragImageSize(186, 20);
    18. QImage dragImage(dragImageSize, QImage::Format_ARGB32_Premultiplied);
    19.  
    20. QPainter dragPainter(&dragImage);
    21.  
    22. dragPainter.setCompositionMode(QPainter::CompositionMode_Source);
    23. dragPainter.fillRect(dragImage.rect(), Qt::transparent);
    24.  
    25. dragPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    26. dragPainter.drawPixmap(0, 0, 16, 16, QPixmap::fromImage(icon));
    27. dragPainter.drawText(QRect(20, 0, 170, 20), dragTreeViewItem->text(0) );
    28.  
    29. dragPainter.end();
    30.  
    31. QDrag *drag = new QDrag(this);
    32. drag->setPixmap( QPixmap::fromImage(dragImage) );
    33.  
    34.  
    35. QMimeData *mimeData = new QMimeData;
    36. mimeData->setText(dragTreeViewItem->text(0));
    37. drag->setMimeData(mimeData);
    38. drag->exec(Qt::MoveAction);
    39. }
    40. }
    41.  
    42. QTreeWidget::mouseMoveEvent(event);
    43. }
    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.
    Attached Files Attached Files

Similar Threads

  1. Drag & Drop to Systray icon
    By ahmed.helal in forum Qt Programming
    Replies: 0
    Last Post: 21st December 2010, 16:34
  2. Replies: 3
    Last Post: 10th June 2010, 15:13
  3. Creating a Pixmap for drag and drop
    By Cruz in forum Qt Programming
    Replies: 22
    Last Post: 20th January 2009, 14:51
  4. cursor pixmap during drag&drop
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 15:55
  5. Icon Change in Drag & Drop
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 9th December 2006, 12:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.