Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QPrinter crash on Qt5
QtWS25 Last Chance

[Solved] QPrinter crash on Qt5

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.3k Views
  • 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 Offline
    I Offline
    ichdyan
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      msue
      wrote on last edited by
      #2

      Perhaps you just need a font.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ichdyan
        wrote on last edited by
        #3

        thank you for replying..

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

        but no luck

        1 Reply Last reply
        0
        • M Offline
          M Offline
          msue
          wrote on last edited by
          #4

          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
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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
            0
            • I Offline
              I Offline
              ichdyan
              wrote on last edited by
              #6

              thank you msue and SGaist. solved...

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved