Results 1 to 8 of 8

Thread: get a pixmap for a dragged QTreeWidgetItem?

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default get a pixmap for a dragged QTreeWidgetItem?

    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.

  2. #2
    Join Date
    Sep 2011
    Location
    Mannheim, Germany
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: get a pixmap for a dragged QTreeWidgetItem?

    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();

  3. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: get a pixmap for a dragged QTreeWidgetItem?

    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 indicated
    Qt Code:
    1. drag->setPixmap(yourPixmap)
    To copy to clipboard, switch view to plain text mode 
    but how you create yourPixmap with the caption of the QTreeWidgetItem?

    Carlos.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: get a pixmap for a dragged QTreeWidgetItem?

    Can't you use QPainter to draw text on the pixmap, before you pass it to QDrag ?

  5. #5
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: get a pixmap for a dragged QTreeWidgetItem?

    I tried that with:

    Qt Code:
    1. QPixmap pixmap;
    2. QPainter paint(&pixmap);
    3. QFont font;
    4. QFontMetrics met(font);
    5. paint.drawText(met.boundingRect("Some text here"),Qt::AlignLeft,"Some text here");
    6. mimeData = new QMimeData;
    7. mimeData->setData("object/x-IMPACT-plugin", itemData);
    8. mimeData->setText("Some text here");
    9. drag = new QDrag(this);
    10. drag->setMimeData(mimeData);
    11. drag->setPixmap(pixmap);
    12. drag->exec(Qt::MoveAction);
    To copy to clipboard, switch view to plain text mode 

    But I still don't get it!

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: get a pixmap for a dragged QTreeWidgetItem?

    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.

  7. The following user says thank you to stampede for this useful post:

    qlands (27th September 2011)

  8. #7
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: get a pixmap for a dragged QTreeWidgetItem?

    ok.

    Almost there, now I get a white pixmap with the appropriate size but without the text.

    Qt Code:
    1. QPixmap pixmap(met.boundingRect("Some text here").size());
    2. pixmap.fill();
    3. QPainter paint(&pixmap);
    4. 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:
    1. QRectF rect(met.boundingRect("Some text here"));
    2. 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.

  9. #8
    Join Date
    Sep 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: get a pixmap for a dragged QTreeWidgetItem?

    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);

Similar Threads

  1. Replies: 1
    Last Post: 19th April 2011, 11:17
  2. QTreeWidgetItem with tooltip+pixmap
    By Zed in forum Qt Programming
    Replies: 1
    Last Post: 21st January 2011, 16:40
  3. Replies: 0
    Last Post: 20th August 2010, 23:39
  4. Replies: 1
    Last Post: 15th August 2009, 11:23
  5. Mouse dragged widget without title
    By Tonal in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2006, 12:28

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.