Qt6 topics merged with General

Problem with two stage painting

  • In my CTOR I coded:

    setBackgroundRole(QPalette::Dark);      // A darker background please

    And then in the paintEvent method:

    QPainter pixPainter(&m_drawingPixmap);
           // lots of code writing to pixmap
        QPainter painter(this);
        painter.drawPixmap(0, 0, m_drawingPixmap);

    which worked fine.

    I cranked up the error level to /W3, and I was told to change pixPainter.initFrom(this), to pixPainter.begin()

    BUT when I made that change, the background colour changed from the nice mid-grey to a nasty glary white.

    So, was the warning correct and if so how do I get my grey background back?

  • Lifetime Qt Champion


    Because your should explicitly setup the pen/brush of your painter.

    Out of curiosity, why that two stage painting ? You are doing all your operation on that QPixmap that is the exact size of your widget and then painting it on that widget at the exact same size. You could do the painting directly on your widget.

  • Because there'll be other stuff writing onto that pixmap so I'm preparing for double buffering it all.

    explicitly set pen/brush

    I don't want to manually set a background brush - I want inherit the dark scheme palette, hence the call to setBackgroundRole(QPalette::Dark);

    If that's not the right way in Qt5 (I lifted that code from a Qt 4 book), how should I do it.


  • I tried this:

            QPainter pixPainter(&m_drawingPixmap);
            QPalette palette{ QGuiApplication::palette() };
            QBrush brush{ palette.dark() };
            pixPainter.fillRect(rect(), brush);

    Which works, but shouldn't setBackgroundRole work too?


  • Lifetime Qt Champion

    That's for your widget which your pixmap has no relation with technically speaking.

    Personally I am used to explicitly setup the painter so it's not something that "shocks" me.

Log in to reply