Russian Qt Forum
Апрель 18, 2024, 15:30 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: [1]   Вниз
  Печать  
Автор Тема: [РЕШЕНО]Печать в отдельном потоке  (Прочитано 3051 раз)
Larry
Гость
« : Январь 29, 2018, 16:26 »

Добрый день, форумчане.
Сделал вывод QTableWidget в PDF и сейчас все работает, но при этом замирает GUI, может на несколько минут (в зависимости от размера таблицы).
Решил вынести в отдельный поток. Сделал следующее:

Код:
#ifndef CTHREAD_H
    #define CTHREAD_H
    //----------------
    #include <QObject>
    #include <QPrinter>
    #include <QTextDocument>
    #include <QDebug>
    //---------------------------
    class CThread: public QObject
    {
        Q_OBJECT

        public:
            explicit CThread(QTextDocument* document, QObject* parent = nullptr);

        signals:
            void finished();

        public slots:
            void print();

        private:
            QTextDocument* m_document;
    };
#endif // CTHREAD_H

Код:
#include "cthread.h"
//---------------------------------------------------------
CThread::CThread(QTextDocument* document, QObject* parent):
    QObject(parent),
    m_document(document)
{

}
//-------------------
void CThread::print()
{
    QPrinter* printer = new QPrinter(QPrinter::ScreenResolution);

    printer->setOutputFormat(QPrinter::PdfFormat);
    printer->setPaperSize(QPrinter::A4);
    printer->setPageMargins(15, 0, 0, 0, QPrinter::Millimeter);
    printer->setOutputFileName("reports/report.pdf");

    m_document->print(printer);

    emit finished();
    delete printer;
}

Запускаю так:
Код:
   // выводим на печать через отдельный поток
    QThread* thread       = new QThread;
    CThread* print_thread = new CThread(reportPDF);

    print_thread->moveToThread(thread);

    connect(thread, &QThread::started, print_thread, &CThread::print);
//    connect(print_thread, &CThread::finished, [this]
//    {

//    });

    thread->start();

Все работает, но получаю вывод предупреждений:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0x22e8d0d0), parent's thread is QThread(0x1b91c010), current thread is QThread(0x22e8d3f0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0x22e8d0d0), parent's thread is QThread(0x1b91c010), current thread is QThread(0x22e8d3f0)
QThread: Destroyed while thread is still running

Как избавиться от этих предупреждений?
Спасибо.
« Последнее редактирование: Январь 29, 2018, 21:14 от Larry » Записан
Larry
Гость
« Ответ #1 : Январь 29, 2018, 21:14 »

Решил вопрос через QtConcurrent::run...
Код:
QFutureWatcher<void> watcher;
connect(&watcher, SIGNAL(finished()), m_status_bar, SLOT(stopProgressbar()));

m_status_bar->startProgressbar();

QFuture<void> future = QtConcurrent::run(this, &exportToPDF);
watcher.setFuture(future);
Записан
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.041 секунд. Запросов: 20.