0

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!

castors33
  • 477
  • 10
  • 26

1 Answers1

2

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
Silas Parker
  • 8,017
  • 1
  • 28
  • 43
  • thanks that really help me! but I still have a weird problem....in your exemple all work fine, but when I try this in my own files, it just flashes : I see it for an half second...it doesn't wait for the answer, it just chooses rejected by itself – castors33 Apr 20 '12 at 13:38
  • I just see that it was reported as a bug...on Mac, the first time we call exe() it just flash...so write a second exe() just after works...I still trying to find another solution...a good one – castors33 Apr 20 '12 at 13:58
  • Is your event loop working correctly? Problems with windows are sometimes caused if you modify the event loop in unusual ways. – Silas Parker Apr 20 '12 at 13:59
  • 1
    Have you called `QApplication::exec()` in main (the normal way), or are you doing unusual things, like manually pumping it by calling `QCoreApplication::processEvents()`. If it's the normal way, then it probably wouldn't be the problem. – Silas Parker Apr 20 '12 at 14:19
  • I really wonder why yours works perfectly if it's supposed to be a bug – castors33 Apr 20 '12 at 14:50
  • I found out that calling `processEvents(QEventLoop::AllEvents);` just before `.exe()` make it works, the only thing is that I have to pass the `QApplication`to my main class [on this site](https://bugreports.qt-project.org/browse/QTBUG-17913?focusedCommentId=174030&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-174030) that's a weird way to make it works...but at least it works... – castors33 Apr 20 '12 at 15:06
  • 1
    You shouldn't need to pass the application object as `processEvents` is static, also the default is `AllEvents`, so the call can be simplified to `QCoreApplication::processEvents();` and you can avoid passing the application object. See http://qt-project.org/doc/qt-4.8/qcoreapplication.html#processEvents – Silas Parker Apr 20 '12 at 15:18