Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with two stage painting



  • In my CTOR I coded:

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

    And then in the paintEvent method:

    QPainter pixPainter(&m_drawingPixmap);
        pixPainter.initFrom(this);
        pixPainter.setRenderHint(QPainter::Antialiasing);
        pixPainter.setRenderHint(QPainter::SmoothPixmapTransform);
        pixPainter.eraseRect(rect());
    
           // lots of code writing to pixmap
    
        pixPainter.end();
    
        QPainter painter(this);
        painter.drawPixmap(0, 0, m_drawingPixmap);
        painter.end();
    

    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

    Hi,

    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.

    Thanks



  • I tried this:

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

    Which works, but shouldn't setBackgroundRole work too?

    David


  • 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