Print text of a QWebview, QPrintDialog doesn't appear

I tried this:

QPrintDialog printdia(MyWevView);

printdia.exec(); 

QPrinter* printer = printdia.printer();

QPainter painter(printer);      

MyWevView->render(&painter, MyWevView->pos(),MyWevView->visibleRegion());

I know I don't check if the user accepts but that's not the problem...everytime, I don't see the dialog, it just prints

so first I don't understand why the QPrintDialog doesn't appear and second why it knows my printer and go directly prints the text

I guess fixing my first problem will solve my second...

thanks!


The following is a complete working example of printing with QWebView , QPrintDialog works fine:

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();
}

test.pro

QT += core gui webkit
SOURCES=test.cpp win.cpp
HEADERS=win.h
链接地址: http://www.djcxy.com/p/39306.html

上一篇: 通过虚拟键盘在qwebview的文本框中插入文本

下一篇: 打印QWebview,QPrintDialog的文本不会出现