Can work QPrinter in a thread?



  • 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...



  • 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...



  • 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?



  • 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.



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



  • [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?



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



  • [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)?



  • 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....



  • 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 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()"...


Log in to reply
 

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