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.
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.
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.
What is the effect you are trying to achieve? What is your exact code including the paint event?
The code below essentially demonstrates the problem. The dragMoveEvent() is processed however no painting occurs.
Qt Code:
#include "Tree.h" #include <QDrag> #include <QDragEnterEvent> #include <QPainter> #include <QDebug> { setDragEnabled(true); setAcceptDrops(true); } { drag->exec(); } { event->accept(); } { qDebug() << "Drag move event"; repaint(); } { painter.setPen(Qt::gray); }To copy to clipboard, switch view to plain text mode
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.
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.
Well then it doesn't show the problem.
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.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.
Qt Code:
#include <QDrag> #include <QDragEnterEvent> #include <QPainter> #include <QDebug> #include <QApplication> #include <QTreeWidget> public: protected: }; { setDragEnabled(true); setAcceptDrops(true); } { qDebug() << Q_FUNC_INFO; drag->exec(); } { qDebug() << Q_FUNC_INFO; event->accept(); } { qDebug() << Q_FUNC_INFO; repaint(); } { qDebug() << Q_FUNC_INFO; painter.setPen(Qt::gray); } int main(int argc, char **argv){ QTreeWidget w; w.show(); return app.exec(); }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 09:43.
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:
#include <QDrag> #include <QDragEnterEvent> #include <QPainter> #include <QDebug> #include <QApplication> #include <QTreeWidget> public: protected: }; { setDragEnabled(true); setAcceptDrops(true); } { qDebug() << Q_FUNC_INFO; drag->exec(); } { qDebug() << Q_FUNC_INFO; event->accept(); } { qDebug() << Q_FUNC_INFO; repaint(); } { qDebug() << Q_FUNC_INFO; painter.setPen(Qt::gray); } int main(int argc, char **argv){ Tree w; item1->setText(0, "Item 1"); item2->setText(0, "Item 2"); item3->setText(0, "Item 3"); item4->setText(0, "Item 4"); w.addTopLevelItem(item1); w.addTopLevelItem(item2); w.addTopLevelItem(item3); w.addTopLevelItem(item4); w.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
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:
public: protected: qDebug() << o->objectName() << v; } }; int main(int argc, char **argv){ App app(argc, argv); Tree w; w.setObjectName("Tree"); w.viewport()->setObjectName("Tree's viewport"); w.installEventFilter(&app); w.viewport()->installEventFilter(&app); //...To copy to clipboard, switch view to plain text mode
Among the results you'll find this:
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:text Code:
"Tree's viewport" "DragMove" virtual void Tree::dragMoveEvent(QDragMoveEvent*) "Tree" "UpdateRequest" "Tree" "Paint"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 11:41.
inktomi (2nd March 2011)
Ah, that makes sense. And that also explains QPainter painter(viewport()) rather than QPainter painter(this).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).
Thanks wysota!
Bookmarks