QPainter does not paint on QPrinter on release build



  • Hello,

    On a debug build painting works fine. I tried building with both MSVC and MinGW.

    Here's a code snippet:

    void ReportPrinter::ShowPreviewDialog(QPrinter::OutputFormat format) {
        m_printer.setOutputFormat(format);
    
        QString selectedPageSize = m_pageSizeCombobox->currentText();
    
        if (selectedPageSize == "A4") {
            m_printer.setPageSize(QPrinter::A4);
        } else if (selectedPageSize == "A3") {
            m_printer.setPageSize(QPrinter::A3);
        } else if (selectedPageSize == "A2") {
            m_printer.setPageSize(QPrinter::A2);
        } else if (selectedPageSize == "A1") {
           m_printer.setPageSize(QPrinter::A1);
        } else if (selectedPageSize == "A0") {
            m_printer.setPageSize(QPrinter::A0);
        }
    
    
        QPrintPreviewDialog prevDlg(&m_printer, nullptr, Qt::Window);
        connect(&prevDlg, SIGNAL(paintRequested(QPrinter*)), this, SLOT(DrawOnPrinter(QPrinter*)));
        prevDlg.exec();
    }
    
    
    void ReportPrinter::DrawOnPrinter(QPrinter *printer) {
        QPainter painter;
        painter.begin(printer);
    
        qDebug() << "painterIsActive: " << painter.isActive(); // returns true on debug build and false on release build
        // painting code ....
    }
    

    Thanks in advance.


  • Qt Champions 2016

    Hi
    Qt version is also important :)



  • Qt version: 5.7.


  • Moderators

    @Enym Does painter.begin(printer) return false? printer is not null in DrawOnPrinter (shouldn't be, but still worth checking)?



  • Had a painter.begin() call in Q_ASSERT, which is obviously disabled in a release build. Stupid me :)

    Q_ASSERT(painter.begin(printer));
    

  • Moderators

    @Enym Yes, don't put production code into asserts :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.