How to detecting finish QEvent::Paint
-
I have class and i cacth event paint and call function. But event run multiple (show, resize...). How do I know that this event is the last event? Please help me!
bool MyWidgetView::event ( QEvent * event )
{
if ( (event->type() == QEvent::Paint))
{
// call function Draw
}
bool rc = QWidget::event(event);
return rc;
} -
Your question is equivalent to "how do I know what happens in the future?" and the answer is: you don't.
Events (especially painting) can be triggered by plethora of stuff, a lot of them outside of your app control.Generally you should react to occurring events and not anticipate them or assume one will come or not.
What are you trying to do exactly? Maybe it's the case of "the wrong question for the right reason"?
-
@HAHAHAHAHA
To add to what Chris Kawa said, you should not depend on the order of events. The events are (usually) generated from the system and there's no guarantee that you get a resize after show, it might be quite the opposite depending on the OS/platform. -
Hi can I ask why you want to know last paint event (even if there is no last)
Also is there a reason that you cannot use the normal paint ?
void paintEvent(QPaintEvent*) -
Thank all.
I'm using an API to draw. But when you finish painting the Paint event of Qt happen so my drawings were obscured. Now i can fix it by call function update draw before 50ms.bool MyClass::event ( QEvent * event ) { if ((event->type() == QEvent::Paint)) { QTimer::singleShot(50, this, SLOT(updateDraw())); } bool rc = QWidget::event(event); return rc; }
Or
void MyClass::paintEvent(QPaintEvent *e) { QTimer::singleShot(50, this, SLOT(updateDraw())); }
But it is not a good way. Because it was called several times. :((
-
@HAHAHAHAHA said:
void MyClass::paintEvent(QPaintEvent *e)
{
QTimer::singleShot(50, this, SLOT(updateDraw()));
}Don't do that. That's not how painting is suppose to work. As I said - paint event is called whenever a widget needs to be repainted. This can happen because of a window resize, because it gained focus, state of some widget changed or something called
update()
orrepaint()
. There are a lot more possible triggers. And yes, this will happen multiple times during your app lifetime and quite often. In general, you can't anticipate when it happens, but whenever it does you should paint immediately current state of the widget and as quickly as possible. If you want to force painting at some point callupdate()
and Qt will schedule a paint event.What are you trying to do exactly? It looks like you're fighting against the framework.
-
@mrjj Hi, we already used the method void paintEvent(QPaintEvent*) as you suggested, but my drawing did not display.
The reason that we ask the last paint event is to put call method specify drawing (it is supported by third part api) after update() of Qt. In case drawing big data then drawing executing called many, so performance of its very slowly.
Pls help us and give any advance! -
@_Hien_VN
Hi , i think we need more info on how you draw this other drawing
as it seems to be the key to understand what you are doing.Beware that before a paint event, the widgets area is cleared with background color.
I wonder if this is why your drawing dont show.
u can disable it with
widget->setAttribute(Qt::WA_NoSystemBackground);If that dont do it for you , u need to show the code for updateDraw as you can not in
any good way know when the last paint event is.
It will never be a solid solution. :) -
@mrjj Hi, we tried it as your suggestion with flow logic as below:
- initial widget, then widget->setAttribute(...)
- call update()
- call paint()
OR - initial widget, then widget->setAttribute(...)
- call paint()
- call update()
The result not change, drawing flicker then not display.