Results 1 to 20 of 23

Thread: Creating a Pixmap for drag and drop

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a Pixmap for drag and drop

    did you set 'setAcceptDrops(true)'?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a Pixmap for drag and drop

    I can drag and drop the frame into a QLineEdit and it will show whatever I set as text into the MimeData. So I assume this line edit has its accept drops set to true. The drag itself works fine. The problem is only related to what the drag looks like. In particular I don't seem to be able to draw on the pixmap. I don't thing the accept drops has anyhting to do with it.

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a Pixmap for drag and drop

    this example works fine for me, but it is not complite, just demo for creating and moving grabbed image.
    Qt Code:
    1. #include <QtGui>
    2. #include "test.h"
    3.  
    4. Test::Test(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. QWidget *widget = new QWidget;
    8. QGridLayout *grid = new QGridLayout(widget);
    9.  
    10. QPushButton *pb = new QPushButton(tr("Button"));
    11. pb->installEventFilter(this);
    12. QComboBox *cb = new QComboBox;
    13. cb->installEventFilter(this);
    14. QCheckBox *chb = new QCheckBox(tr("Check box"));
    15. chb->installEventFilter(this);
    16. QRadioButton *rb = new QRadioButton(tr("Radio button"));
    17. rb->installEventFilter(this);
    18. QSpinBox *sb = new QSpinBox;
    19. sb->installEventFilter(this);
    20. dsp->installEventFilter(this);
    21.  
    22. grid->addWidget(pb, 0, 0);
    23. grid->addWidget(cb, 0, 1);
    24. grid->addWidget(chb, 0, 2);
    25. grid->addWidget(rb, 1, 0);
    26. grid->addWidget(sb, 1, 1);
    27. grid->addWidget(dsp, 1, 2);
    28.  
    29. setCentralWidget(widget);
    30.  
    31. setAcceptDrops(true);
    32. }
    33.  
    34. void Test::mousePressEvent(QMouseEvent *event)
    35. {
    36. processDrag(this, event);
    37. }
    38.  
    39. void Test::processDrag(QWidget *widget, QMouseEvent *event)
    40. {
    41. QPixmap pixmap = QPixmap::grabWidget(widget);
    42. QPainter painter(&pixmap);
    43. painter.setPen(Qt::red);
    44. painter.drawText(0, pixmap.rect().center().y(), "hello Qt");
    45.  
    46. QByteArray itemData;
    47. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    48. dataStream << pixmap << QPoint(event->pos());
    49.  
    50. QMimeData *mimeData = new QMimeData;
    51. mimeData->setData("application/x-dnditemdata", itemData);
    52.  
    53. QDrag *drag = new QDrag(this);
    54. drag->setMimeData(mimeData);
    55. drag->setPixmap(pixmap);
    56. drag->setHotSpot(event->pos());
    57.  
    58. drag->exec();
    59. }
    60.  
    61. bool Test::eventFilter(QObject *o, QEvent *e)
    62. {
    63. if (e->type() == QEvent::MouseButtonPress) {
    64. QMouseEvent *me = static_cast<QMouseEvent *>(e);
    65. QWidget *widget = qobject_cast<QWidget *>(o);
    66. if (widget && me->button() == Qt::LeftButton) {
    67. processDrag(widget, me);
    68. return true;
    69. }
    70. }
    71.  
    72. return QMainWindow::eventFilter(o, e);
    73. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. The following user says thank you to spirit for this useful post:

    Cruz (19th January 2009)

  5. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a Pixmap for drag and drop

    Wow! Thanks a lot for all the effort you are putting into this. I built a project from your example and it works for me too. Now I need to scrutinize the differences and see which bit is responsible.

  6. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a Pixmap for drag and drop

    I found the bugger! It's the style sheet.

    Qt Code:
    1. background-color: beige;
    2. }
    To copy to clipboard, switch view to plain text mode 

    With this I set the bg color of the application. If I take this out, the drag looks like it should. If this is back in, the drag is only a beige rect.

    Why is the style sheet influencing the drag this way? The drag itself is not a widget is it?

    I tried taking this out of my style sheet and explicitly setting the background of my main window (which is a QWidget and not a QMainWindow in my case) in Designer. It has the same bad effect on the drag.

    So the last thing to figure out is how to set a background color for the application AND have nice looking drags.

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a Pixmap for drag and drop

    try to use QPalette insted of using style sheets.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a Pixmap for drag and drop

    Yes, the palette works. At the end using style sheets brought me more trouble than good. It's nice how you can change the appearance easily by editing a singe style sheet and not even have to recompile for it. But it doesn't look the same on Windows and on Linux and there is troubles like with the drag. There is still nothing like pixel exact coding.

  9. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a Pixmap for drag and drop

    try to update your Qt version, maybe that behavior which you described is a bug. what version do you use?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #9
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a Pixmap for drag and drop

    4.4 I think it's the latest. I just installed it 5 days ago.
    Last edited by Cruz; 19th January 2009 at 16:22. Reason: spelling error

  11. #10
    Join Date
    Nov 2008
    Posts
    33
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a Pixmap for drag and drop

    Thanks for this interesting thread chaps; I just wanted to say that I had a similar experience with dragging pixmaps being messed up by stylesheets (I gave up on the fancy stuff in the end and just settled for the standard little drag box). If I remember correctly, I also had problems displaying tooltips for widgets which had stylesheets. There was a note somewhere in the documentation warning against mixing palette and stylesheet effects which is fair enough I suppose but stylesheets on their own cause unexpected results sometimes. I try to avoid them now.

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
  •  
Qt is a trademark of The Qt Company.