Qt Forum

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

    [Solved] QPrinter crash on Qt5

    General and Desktop
    3
    6
    1950
    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.
    • I
      ichdyan last edited by

      Hi, i need help...
      i write small program to print receipt. but having difficulties with Qt5. It always crash. in Qt4 it working just fine. So i decide to test it, here is the code:

      @#include <QCoreApplication>
      #include <QPrinter>
      #include <QPainter>

      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);

      QPrinter printer;
      printer.setOutputFormat(QPrinter::PdfFormat);
      printer.setOutputFileName("test.pdf");
      
      QPainter p;
      if (p.begin(&printer))
      {
          p.drawText(0, 0, "test");
          p.end();
      }
      
      return 0;
      

      }@

      in Qt5 if i replace drawText with drawLine, crash didn't happen. Tested on Qt 5.3.1 MSVC 2013 and Qt 5.2.1 MSVC 2010, Qt 4.8.6 MSVC 2010. What is wrong with my code? Can someone give me a hint about the proper way to use QPrinter on Qt5. I'm just newbie on programming...
      Thank you. Sorry for my bad english.

      1 Reply Last reply Reply Quote 0
      • M
        msue last edited by

        Perhaps you just need a font.

        1 Reply Last reply Reply Quote 0
        • I
          ichdyan last edited by

          thank you for replying..

          added
          @ QFont font("Arial");
          p.setFont(font);@

          but no luck

          1 Reply Last reply Reply Quote 0
          • M
            msue last edited by

            I tested it: my debugger complained about a missing font and it wanted a QGuiApplication instead of a QCoreApplication. Then it worked on my machine.

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

              Hi and welcome to devnet,

              IIRC you must use a QApplication

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 0
              • I
                ichdyan last edited by

                thank you msue and SGaist. solved...

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