class PrintThread : public QThread{ Q_OBJECTpublic: PrintThread(QWebView*, QPrinter*);protected: void run(); QWebView *wv; QPrinter *printer;};void PrintThread::run(){ wv->print(printer);}
QWebView *webview = new QWebView();QPrinter *printer = new QPrinter();//...PrintThread ptPrint(webview, printer);ptPrint->start();
class PrintThread : public QThread{ Q_OBJECTpublic: PrintThread(QObject * parent = 0); void start(const QString &); QPrinter printer_; QWebPage *wpage_; QPrinter *printer(){return &printer_;};protected: void run();};
PrintThread *ptPrint = new PrintThread(this); QPrintDialog dlg(ptPrint->printer(), this); if (dlg.exec() == QDialog::Accepted) { connect(ptPrint, SIGNAL(finished()), ptPrint, SLOT(deleteLater())); ptPrint->start(html); } else { delete ptPrint; }
void PrintThread::start(const QString & html){ html_ = html; wpage_ = new QWebPage(this); wpage_->moveToThread(this); wpage_->mainFrame()->setHtml(html_); QThread::start();}
void PrintThread::run(){ wpage_->mainFrame()->print(&printer_);}
void PrintThread::start(const QString & html){ html_ = html; wpage_ = new QWebPage(this); wframe_ = wpage_->mainFrame(); wframe_->setHtml(html_); wframe_->moveToThread( this ); QThread::start();}void PrintThread::run(){ wframe_->print(&printer_);}
class PrintThread : public QThread{ QPrinter _printer; QWebFrame *_frame;public: PrintThread(QObject *parent) : QThread(parent), _printer(QPrinter::HighResolution), _frame(0) { } ~PrintThread() { wait(); } QPrinter *printer() { return &_printer; } void start(const QString &html) { _frame = new QWebFrame(); //парент не указываем! _frame->moveToThread(this); _frame->setHtml(html); QThread::start(); }protected: void run() { _frame->print(printer()); delete _frame; _frame = 0; }};void <some_class>::print(){ if (!QFontDatabase::supportsThreadedFontRendering()) { QPrinter printer(QPrinter::HighResolution); QPrintDialog dlg(&printer, this); if (dlg.exec() == QDialog::Accepted) { qApp->setOverrideCursor(Qt::WaitCursor); _webView->print(&printer); qApp->restoreOverrideCursor(); } return; } PrintThread *thread = new PrintThread(this); QPrintDialog dlg(thread->printer(), this); if (dlg.exec() == QDialog::Accepted) { connect(thread, SIGNAL(finished()), SLOT(printingFinished())); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); qApp->setOverrideCursor(Qt::BusyCursor); thread->start(_webView->page()->mainFrame()->toHtml()); } else { delete thread; }}void <some_class>::printingFinished(){ qApp->restoreOverrideCursor();}