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. Can work QPrinter in a thread?
QtWS25 Last Chance

Can work QPrinter in a thread?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 5.5k 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.
  • G Offline
    G Offline
    gronerth
    wrote on last edited by
    #1

    Hello guys,

    I need to generate pdf files but i need to generate it in background because it may freeze the application. I have used the following functions in a thread:

    @
    editor = new QTextDocument();
    printer = new QPrinter(QPrinter::ScreenResolution);
    printer->setOutputFormat(QPrinter::PdfFormat);

    editor->clear();
    editor->setHtml(dataHtml);
    
    QDir path("./Reports/");
    printer->setOutputFileName(path.absolutePath() + QString("/")+fileName);
    editor->print(printer);
    

    @

    All the objects were created in the thread instance. When i try to execute the code, i have always the following error: QPainter::begin(): Returned false. So, i was wondering if QPrinter can't work in a thread, if it is so, does someone have any suggestions to achieve the same result?

    Thanks in advance...


    JETG

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gronerth
      wrote on last edited by
      #2

      As far as i can see, if all those operations were made in the run() function it works, but if it is made in another function doesnt works...

      I mean..This kind of code works:

      @
      void run()
      {
      //Create the objects
      //Generates the PDF file...
      }
      @

      But this doesnt:

      @
      void run()
      {
      //create the objects
      generatePDFs();
      }

      void generatePDfs() // <--- this function is part of the class..
      {
      //generates the pdf file <-- here i get the error of the QPainter...
      }
      @

      I can't see where is the problem, maybe i am using wrong the QThread class...


      JETG

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        That sounds strange. So it's not a problem of doing it inside a thread, it#s a problem of function separation?

        Then please give aus more code. a small, (not) working example with all needed code. What exactly is done in run, what in generatePdfs?

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          This could be an instance of the "GUI only in the main thread" problem (painting on a QPrinter is GUI, as far as I know).

          Is there any output on the console? As far as I know, QPainter prints some debugging messages when it fails.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            What iritated me is, he said uif everything is in run, it works....
            run is the thread function...

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              [quote author="Gerolf" date="1302169254"]What iritated me is, he said uif everything is in run, it works....
              run is the thread function...[/quote]

              An instance of the "run in the context where the method is called" problem?

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                That's why I asked for more code...

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  alexKI
                  wrote on last edited by
                  #8

                  [quote author="gronerth" date="1302133641"]As far as i can see, if all those operations were made in the run() function it works, but if it is made in another function doesnt works...[/quote]
                  do you use the constructor to initialize objects for this functions (parent thread for this objects will be thread where class was created)?

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gronerth
                    wrote on last edited by
                    #9

                    To give an small example, this kind of code doesn't work:

                    @
                    class reportGeneratorThread : public QThread
                    {
                    Q_OBJECT
                    public:
                    void run();
                    private:
                    QPrinter *printer;
                    QTextDocument *editor;
                    public slots:
                    void makeReports();
                    }

                    reportGeneratorThread::reportGeneratorThread(QObject *parent) :
                    QThread(parent)
                    {

                    }

                    void reportGeneratorThread::run()
                    {
                    editor = new QTextDocument();
                    editor->setHtml("<h1>hola</h1>");
                    printer = new QPrinter(QPrinter::HighResolution);
                    printer->setOutputFormat(QPrinter::PdfFormat);
                    printer->setOutputFileName("report.pdf");
                    makeReports();
                    }

                    void reportGeneratorThread::makeReports()
                    {
                    editor->print(printer);
                    }
                    @

                    [quote author="Volker" date="1302167511"]This could be an instance of the "GUI only in the main thread" problem (painting on a QPrinter is GUI, as far as I know).

                    Is there any output on the console? As far as I know, QPainter prints some debugging messages when it fails.[/quote]

                    The debug message is: QPainter::begin(): Returned false

                    but if i put the code editor->print(printer) inside the run function it works....


                    JETG

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      webmaster.skelton
                      wrote on last edited by
                      #10

                      I believe that Volker is correct, aa QPrinter inherits from QPaintDevice, though I have not found anything that explicitly says it is always "thunked" back into the GUI thread. This may also be a stupid question, but when you call the thread you call it by using "start()" and not "run()" correct? As this will also cause strange behavior.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        gronerth
                        wrote on last edited by
                        #11

                        [quote author="webmaster.skelton" date="1302199366"]I believe that Volker is correct, aa QPrinter inherits from QPaintDevice, though I have not found anything that explicitly says it is always "thunked" back into the GUI thread. This may also be a stupid question, but when you call the thread you call it by using "start()" and not "run()" correct? As this will also cause strange behavior. [/quote]

                        yes, i call it with "start()"...


                        JETG

                        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