以下は、正常QWebView
に動作する を使用した印刷の完全な動作例です。QPrintDialog
win.h
#ifndef _WIN_H_
#define _WIN_H_
#include <QtGui>
#include <QtWebKit>
class TestWindow : public QMainWindow
{
Q_OBJECT
public:
TestWindow();
private slots:
void doPrint();
void doPrintVis();
private:
QWebView* mView;
Q_DISABLE_COPY(TestWindow)
};
#endif
win.cpp
#include "win.h"
TestWindow::TestWindow() :
QMainWindow(),
mView(new QWebView())
{
setCentralWidget(mView); // Takes ownership
mView->load(QUrl::fromUserInput("http://qt-project.org/doc/qt-4.8/qwebview.html"));
QToolBar* tools = addToolBar("Tools");
QAction* printAction = tools->addAction("Print");
QAction* printVisAction = tools->addAction("Print Visible");
connect(printAction, SIGNAL(triggered(bool)), this, SLOT(doPrint()));
connect(printVisAction, SIGNAL(triggered(bool)), this, SLOT(doPrintVis()));
}
void TestWindow::doPrint()
{
qDebug() << "TestWindow::doPrint()";
QPrinter printer;
QPrintDialog printDlg(&printer);
if (printDlg.exec() == QDialog::Rejected)
return;
mView->print(&printer);
}
void TestWindow::doPrintVis()
{
qDebug() << "TestWindow::doPrintVis()";
QPrinter printer;
QPrintDialog printDlg(&printer);
if (printDlg.exec() == QDialog::Rejected)
return;
QPainter painter(&printer);
mView->render(&painter, mView->pos(), mView->visibleRegion());
}
test.cpp
#include <QtGui>
#include <QtWebKit>
#include "win.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
TestWindow win;
win.show();
return app.exec();
}
テストプロ
QT += core gui webkit
SOURCES=test.cpp win.cpp
HEADERS=win.h