How can I make sure that paintEvent has finished?
-
Hi,
in my applicaiton I am invoking the paintEvent through the udpate() methode. Inside the paintEvent I am using a painter to draw a rectangle. After each invocation of the update() methode I am changing the coordinates so that the rectangle is moved meandering through the widget. After each move of the rectangle I need to take a picture of the screen through a camera which is positioned in the front of the monitor. The code for taking pictures is running on a separate thread. GUI-Thread and "camera"-Thread are synchronized through a conditional variable.
Now I have some problems in synchronizing between the threads. It happens that I am taking two pictures of the same rectangle position and afterwards the rectangle moves twice...
I think it has something to do with the paintEvent and the conditional variable. How can I make sure that paintEvent has finished (rectangle is moved!). I also need to somehow filter that paintEvent has finished based on my invocation of the update() methode and not something different triggering the paintEvent.
Thank you a lot!
-
Hi,
in my applicaiton I am invoking the paintEvent through the udpate() methode. Inside the paintEvent I am using a painter to draw a rectangle. After each invocation of the update() methode I am changing the coordinates so that the rectangle is moved meandering through the widget. After each move of the rectangle I need to take a picture of the screen through a camera which is positioned in the front of the monitor. The code for taking pictures is running on a separate thread. GUI-Thread and "camera"-Thread are synchronized through a conditional variable.
Now I have some problems in synchronizing between the threads. It happens that I am taking two pictures of the same rectangle position and afterwards the rectangle moves twice...
I think it has something to do with the paintEvent and the conditional variable. How can I make sure that paintEvent has finished (rectangle is moved!). I also need to somehow filter that paintEvent has finished based on my invocation of the update() methode and not something different triggering the paintEvent.
Thank you a lot!
Hi @JamalNewtron ,
There's an alternative to update(), it's to call repaint(), it initiates a instant redraw of the widget instead of passing through the event loop.
Maybe worth a try ? -
Hi,
in my applicaiton I am invoking the paintEvent through the udpate() methode. Inside the paintEvent I am using a painter to draw a rectangle. After each invocation of the update() methode I am changing the coordinates so that the rectangle is moved meandering through the widget. After each move of the rectangle I need to take a picture of the screen through a camera which is positioned in the front of the monitor. The code for taking pictures is running on a separate thread. GUI-Thread and "camera"-Thread are synchronized through a conditional variable.
Now I have some problems in synchronizing between the threads. It happens that I am taking two pictures of the same rectangle position and afterwards the rectangle moves twice...
I think it has something to do with the paintEvent and the conditional variable. How can I make sure that paintEvent has finished (rectangle is moved!). I also need to somehow filter that paintEvent has finished based on my invocation of the update() methode and not something different triggering the paintEvent.
Thank you a lot!
@JamalNewtron
It depends on your operating system, how a screen asset is actually rendered onto the screen. With the info about your app being a bit too general for exact instructions, I propose to emit a signal to the camera thread, when the painting has been done.That can be achieved with an event filter or by subclassing the widget and overriding its paint event. You can emit a signal after having called the paint event of the base class.
The limitation is, that the drawing happens into a backing store.
That may be flushed to the screen asynchronously e.g. on Linux/X11.
So even when the paint event has been completed, the display manager may still flush it at a later point. -
@mpergand: Will give it a try. Thanks a lot.
@Axel-Spoerl: I think that is what I am experiencing. After the paintEvent or the painter.end() method the monitor is not instantly refreshing. I am using Windows 11. Is there any option to get to know when the screen has been flushed? -
@mpergand: Will give it a try. Thanks a lot.
@Axel-Spoerl: I think that is what I am experiencing. After the paintEvent or the painter.end() method the monitor is not instantly refreshing. I am using Windows 11. Is there any option to get to know when the screen has been flushed?@JamalNewtron said in How can I make sure that paintEvent has finished?:
Is there any option to get to know when the screen has been flushed?
My Windows knowledge is very limited. From what I know, things a pretty synchronous on Windows. The issue you experience is not Qt related. It applies to all windows in Windows.