Nominate our 2022 Qt Champions!

Twice QPainter on same QWindow causes crash

  • By mistake I have created twice QPainter on the same QWindow, causing a crash when calling setBrush on the second one.

    The crash occurs inside QPainter::setBrush when calling QRasterPaintEngine::brushChanged, because in the line :

     QRasterPaintEngineState *s = state();

    the variable s is returned as zero (0), resulting in a crash when it is used.

    I have corrected my mistake and do not do that any more.
    However, I have not found it mentioned in the documentation that two QPainters are not allowed, so I'm wondering if this is a bug, or a feature that needs to be documented. In the later case, a more graceful error might also be useful.

  • Moderators

    Is it possible you're trying to paint when you're not allowed to?
    For the QWindow class there's the limitation:

    The application can start rendering as soon as isExposed() returns true, and can keep rendering until it isExposed() returns false. To find out when isExposed() changes, reimplement exposeEvent(). The window will always get a resize event before the first expose event.

    while for widgets you're supposed to paint inside the paint events:

    Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent().

    Kind regards.

  • @kshegunov

    The problem is not that simple - the painting is now ok since I started using only one painter. In any case, a crash is always to be avoided.

    The isExposed() limitation is true, but logic says that an unexposed window does not get paint events. But I digress - this has no bearing on the crash.

    Very kind regards.

  • I have posted this as bug : QTBUG-51624.

  • Moderators

    It's not very well exposed, but this is actually documented in QPainter::begin():

    Warning: A paint device can only be painted by one painter at a time.