Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved QPainter does not paint on QPrinter on release build

    General and Desktop
    3
    6
    520
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E
      Enym last edited by Enym

      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.

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi
        Qt version is also important :)

        1 Reply Last reply Reply Quote 0
        • E
          Enym last edited by Enym

          Qt version: 5.7.

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @Enym last edited by

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 1
            • E
              Enym last edited by Enym

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

              Q_ASSERT(painter.begin(printer));
              
              jsulm 1 Reply Last reply Reply Quote 3
              • jsulm
                jsulm Lifetime Qt Champion @Enym last edited by

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

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 4
                • First post
                  Last post