Can I put the painter into the class variables? :
protected: QPainter *myPainter; ... void MyWidget::paintEvent(QPaintEvent *event) { myPainter = new QPainter(this);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A new anser to address more reentrancy more specifically…
danatel left the following comment to this message (in part):
The answer to this is that it should likely be acceptable, unless you do something really odd. (And if you do something that odd, Qt will likely warn you or abort.) I think there might still be some confusion over what you mean by reentrant, but generating a paintEvent won’t stop the execution flow of the current action to immediately process that event. Instead (like all events), it will be queued up for later processing. As long as you aren’t doing multi-threading or calling processEvents, the execution order of the code while you are in one of your own functions should be very straightforward.
As an example, let’s follow your steps and examine them in more detail.
Foo::paintEvent()handler creates a QPainter and setsFoo::m_painter_pat it.Foo::paintEvent()callsFoo::paintAntarticaFlag().Foo::paintAntarticaFlag(): a) usesFoo::m_painter_p, then b) calls something that callsFoo::update(), then c) usesFoo::m_painter_psome more.Foo::update(), which is really a Qt method, generates a paintEvent for Foo.The above sequence is fine, since update creates an event, which means delayed processing. If instead you called Foo::repaint(), that would cause an immediate recursion into Foo::paintEvent(), which would either cause Qt to abort because you are creating more than 1 painter for the same object, or your program to abort because it eventually (you know, in a few hundred milliseconds) blew out the stack.
If you are doing multiple threads and just want to trigger a redraw, you can still do that from the other thread, since it will just put a paintEvent on the queue to be handled by the proper thread at the proper time. If you are doing multiple threads and want to draw those flags using the same painter, well, don’t. Just don’t. In that case, you might consider drawing each flag to a shared image, and drawing that image where you are using the QPainter now.