Results 1 to 14 of 14

Thread: Painting during dragging

  1. #1
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Painting during dragging

    Hi,

    Is it possible to cause a paintEvent during a drag operation? I'm calling repaint() from the dragMoveEvent() method but paintEvent() is not being called.

    Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Painting during dragging

    dragMoveEvent() will only be invoked if dragEnterEvent() accepted the drag in the first place.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Painting during dragging

    Quote Originally Posted by wysota View Post
    dragMoveEvent() will only be invoked if dragEnterEvent() accepted the drag in the first place.
    I'm actually getting the dragMoveEvent() which I can confirm from the stream of qDebug) output. The problem is a call to repaint() within dragMoveEvent() has no effect whatsoever.

    This happens to be a subclass of QTreeWidget and if I do myTreeWidgetItem->setExpanded() the paintEvent() gets triggered. Only explicit calls to update() and repaint() have no effect.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Painting during dragging

    What is the effect you are trying to achieve? What is your exact code including the paint event?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Painting during dragging

    Quote Originally Posted by wysota View Post
    What is the effect you are trying to achieve? What is your exact code including the paint event?
    I want to update the QTreeWidget with feedback to reflect the effect of the drag operation. I will post a small demo later on today.

  6. #6
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Painting during dragging

    The code below essentially demonstrates the problem. The dragMoveEvent() is processed however no painting occurs.

    Qt Code:
    1. #include "Tree.h"
    2.  
    3. #include <QDrag>
    4. #include <QDragEnterEvent>
    5. #include <QPainter>
    6. #include <QDebug>
    7.  
    8. Tree::Tree(QWidget *parent) :
    9. QTreeWidget(parent)
    10. {
    11. setDragEnabled(true);
    12. setAcceptDrops(true);
    13. }
    14.  
    15. void Tree::mousePressEvent(QMouseEvent *event)
    16. {
    17. QDrag *drag = new QDrag(this);
    18. drag->setMimeData(new QMimeData());
    19. drag->exec();
    20. }
    21.  
    22. void Tree::dragEnterEvent(QDragEnterEvent *event)
    23. {
    24. event->accept();
    25. }
    26.  
    27. void Tree::dragMoveEvent(QDragMoveEvent *)
    28. {
    29. qDebug() << "Drag move event";
    30.  
    31. repaint();
    32. }
    33.  
    34. void Tree::paintEvent(QPaintEvent *event)
    35. {
    36. QTreeWidget::paintEvent(event);
    37. QPainter painter(viewport());
    38.  
    39. painter.setPen(Qt::gray);
    40. painter.drawEllipse(QPointF(qrand() % width(), qrand() % height()), 5, 5);
    41. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Painting during dragging

    How do you know that no painting occurs? BTW. use update() instead of repaint(). And add a qDebug() statement in the paint event to see if it gets called. In my opinion it does get called only that you don't see the ellipse.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Painting during dragging

    Quote Originally Posted by wysota View Post
    How do you know that no painting occurs? BTW. use update() instead of repaint(). And add a qDebug() statement in the paint event to see if it gets called. In my opinion it does get called only that you don't see the ellipse.
    I already tried doing the qDebug() in the paintEvent() and it's not getting called.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Painting during dragging

    To be honest nothing in your code gets called. I don't see a point in reimplementing events of the view if 99% of the view is occupied by other widgets (viewport, header, scroll bars).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Painting during dragging

    Quote Originally Posted by wysota View Post
    To be honest nothing in your code gets called. I don't see a point in reimplementing events of the view if 99% of the view is occupied by other widgets (viewport, header, scroll bars).
    This is not the actual code. It is just a demo that shows the problem.

    What do you mean nothing gets called? The drag events are working fine, the problem seems to be that the paint event does not get called whilst dragging.

    I've seen someone with the same problem on some other mailing list. I'm now starting to think this is a Qt bug.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Painting during dragging

    Quote Originally Posted by inktomi View Post
    This is not the actual code. It is just a demo that shows the problem.
    Well then it doesn't show the problem.

    What do you mean nothing gets called? The drag events are working fine, the problem seems to be that the paint event does not get called whilst dragging.
    This is a direct wrap of your code to make it compilable, run it and see if you get any output. If you want help, provide something meaningful.
    Qt Code:
    1. #include <QDrag>
    2. #include <QDragEnterEvent>
    3. #include <QPainter>
    4. #include <QDebug>
    5. #include <QApplication>
    6. #include <QTreeWidget>
    7.  
    8. class Tree : public QTreeWidget {
    9. public:
    10. Tree(QWidget *parent = 0);
    11. protected:
    12. void mousePressEvent(QMouseEvent*);
    13. void dragEnterEvent(QDragEnterEvent*);
    14. void dragMoveEvent(QDragMoveEvent*);
    15. void paintEvent(QPaintEvent*);
    16. };
    17.  
    18. Tree::Tree(QWidget *parent) :
    19. QTreeWidget(parent)
    20. {
    21. setDragEnabled(true);
    22. setAcceptDrops(true);
    23. }
    24.  
    25. void Tree::mousePressEvent(QMouseEvent *event)
    26. {
    27. qDebug() << Q_FUNC_INFO;
    28. QDrag *drag = new QDrag(this);
    29. drag->setMimeData(new QMimeData());
    30. drag->exec();
    31. }
    32.  
    33. void Tree::dragEnterEvent(QDragEnterEvent *event)
    34. {
    35. qDebug() << Q_FUNC_INFO;
    36. event->accept();
    37. }
    38.  
    39. void Tree::dragMoveEvent(QDragMoveEvent *)
    40. {
    41. qDebug() << Q_FUNC_INFO;
    42.  
    43. repaint();
    44. }
    45.  
    46. void Tree::paintEvent(QPaintEvent *event)
    47. {
    48. qDebug() << Q_FUNC_INFO;
    49. QTreeWidget::paintEvent(event);
    50. QPainter painter(viewport());
    51.  
    52. painter.setPen(Qt::gray);
    53. painter.drawEllipse(QPointF(qrand() % width(), qrand() % height()), 5, 5);
    54. }
    55.  
    56.  
    57. int main(int argc, char **argv){
    58. QApplication app(argc, argv);
    59. w.show();
    60. return app.exec();
    61. }
    To copy to clipboard, switch view to plain text mode 

    And it's not a bug in Qt, it's a bug in your thinking, if I understand the problem correctly. But before I share what I know, I want to see the situation clearly because the code you posted doesn't describe it.
    Last edited by wysota; 2nd March 2011 at 10:43.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Painting during dragging

    It seems there are some misunderstandings. I modified your code snippet to add a few items to the tree widget. Also your snippet creates an instance of QTreeWidget rather than Tree.

    Qt Code:
    1. #include <QDrag>
    2. #include <QDragEnterEvent>
    3. #include <QPainter>
    4. #include <QDebug>
    5. #include <QApplication>
    6. #include <QTreeWidget>
    7.  
    8. class Tree : public QTreeWidget {
    9. public:
    10. Tree(QWidget *parent = 0);
    11. protected:
    12. void mousePressEvent(QMouseEvent*);
    13. void dragEnterEvent(QDragEnterEvent*);
    14. void dragMoveEvent(QDragMoveEvent*);
    15. void paintEvent(QPaintEvent*);
    16. };
    17.  
    18. Tree::Tree(QWidget *parent) :
    19. QTreeWidget(parent)
    20. {
    21. setDragEnabled(true);
    22. setAcceptDrops(true);
    23. }
    24.  
    25. void Tree::mousePressEvent(QMouseEvent *event)
    26. {
    27. qDebug() << Q_FUNC_INFO;
    28. QDrag *drag = new QDrag(this);
    29. drag->setMimeData(new QMimeData());
    30. drag->exec();
    31. }
    32.  
    33. void Tree::dragEnterEvent(QDragEnterEvent *event)
    34. {
    35. qDebug() << Q_FUNC_INFO;
    36. event->accept();
    37. }
    38.  
    39. void Tree::dragMoveEvent(QDragMoveEvent *)
    40. {
    41. qDebug() << Q_FUNC_INFO;
    42.  
    43. repaint();
    44. }
    45.  
    46. void Tree::paintEvent(QPaintEvent *event)
    47. {
    48. qDebug() << Q_FUNC_INFO;
    49. QTreeWidget::paintEvent(event);
    50. QPainter painter(viewport());
    51.  
    52. painter.setPen(Qt::gray);
    53. painter.drawEllipse(QPointF(qrand() % width(), qrand() % height()), 5, 5);
    54. }
    55.  
    56.  
    57. int main(int argc, char **argv){
    58. QApplication app(argc, argv);
    59. Tree w;
    60.  
    61. QTreeWidgetItem *item1 = new QTreeWidgetItem(&w);
    62. item1->setText(0, "Item 1");
    63. QTreeWidgetItem *item2 = new QTreeWidgetItem(&w);
    64. item2->setText(0, "Item 2");
    65. QTreeWidgetItem *item3 = new QTreeWidgetItem(&w);
    66. item3->setText(0, "Item 3");
    67. QTreeWidgetItem *item4 = new QTreeWidgetItem(&w);
    68. item4->setText(0, "Item 4");
    69. w.addTopLevelItem(item1);
    70. w.addTopLevelItem(item2);
    71. w.addTopLevelItem(item3);
    72. w.addTopLevelItem(item4);
    73.  
    74. w.show();
    75. return app.exec();
    76. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Painting during dragging

    Ah, my bad with the Tree vs QTreeWidget thing. But then I'm right about the cause, the paint event does get triggered.

    Add this code to your test app:
    Qt Code:
    1. class App : public QApplication {
    2. public:
    3. App(int argc, char **argv) : QApplication(argc, argv){}
    4. protected:
    5. bool eventFilter(QObject *o, QEvent *e){
    6. QString v = QEvent::staticMetaObject.enumerator(0).valueToKey(e->type());
    7. qDebug() << o->objectName() << v;
    8. return QApplication::eventFilter(o,e);
    9. }
    10.  
    11. };
    12.  
    13. int main(int argc, char **argv){
    14. App app(argc, argv);
    15. Tree w;
    16. w.setObjectName("Tree");
    17. w.viewport()->setObjectName("Tree's viewport");
    18. w.installEventFilter(&app);
    19. w.viewport()->installEventFilter(&app);
    20. //...
    To copy to clipboard, switch view to plain text mode 

    Among the results you'll find this:
    text Code:
    1. "Tree's viewport" "DragMove"
    2. virtual void Tree::dragMoveEvent(QDragMoveEvent*)
    3. "Tree" "UpdateRequest"
    4. "Tree" "Paint"
    To copy to clipboard, switch view to plain text mode 
    so in fact you see that the paint event for the tree does get called after every drag move event. The thing is it doesn't end up in calling paintEvent(). Why? Because paintEvent() is called when the view's viewport is updated and not the view itself (just like with DragMove and every other event handler for abstract scroll areas). And that's exactly what you want - if you call:
    Qt Code:
    1. viewport()->update();
    To copy to clipboard, switch view to plain text mode 
    you'll get the result you expect.
    Last edited by wysota; 2nd March 2011 at 12:41.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    inktomi (2nd March 2011)

  15. #14
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Painting during dragging

    so in fact you see that the paint event for the tree does get called after every drag move event. The thing is it doesn't end up in calling paintEvent(). Why? Because paintEvent() is called when the view's viewport is updated and not the view itself (just like with DragMove and every other event handler for abstract scroll areas).
    Ah, that makes sense. And that also explains QPainter painter(viewport()) rather than QPainter painter(this).

    Thanks wysota!

Similar Threads

  1. Dragging a pointer
    By Cruz in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2009, 04:51
  2. How do disable painting while dragging?
    By Disperato in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2009, 18:47
  3. Widget dragging in MDI app
    By zeeeend in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2007, 22:11
  4. dragging
    By mickey in forum Newbie
    Replies: 1
    Last Post: 25th July 2006, 15:05
  5. dragging
    By mickey in forum Newbie
    Replies: 5
    Last Post: 11th March 2006, 01:26

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.