打印QWebview,QPrintDialog的文本不会出现

我试过这个:

QPrintDialog printdia(MyWevView);

printdia.exec(); 

QPrinter* printer = printdia.printer();

QPainter painter(printer);      

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

我知道我不检查用户是否接受,但这不是问题......每次我都看不到对话框,它只是打印

所以首先我不明白为什么QPrintDialog没有出现,其次为什么它知道我的打印机,并直接打印文本

我想解决我的第一个问题将解决我的第二个问题...

谢谢!


以下是使用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();
}

test.pro

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

上一篇: Print text of a QWebview, QPrintDialog doesn't appear

下一篇: How get a focus element in QWebView/QWebPage?