Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ui->webEngineView->page()->print...



  • Could you add any codes to print webpage ?
    Qt 5.12.0 (Clang 6.0 ,buid 64bit)

    // mainwindow.cpp
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWebEngineView>
    #include <QPrinter>
    #include <QPrintDialog>
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->webEngineView->load(QUrl("http://www.qt.io/"));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_actionPrint_triggered()
    {
        QPrinter printDev;
        QPrintDialog dialog(&printDev, this);
        if (dialog.exec() == QDialog::Rejected)
               return;
            ui->webEngineView->page()->print(&printDev, [=](bool){});
    
    
    }
    
    
    
    // mainwindow.h
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_actionPrint_triggered();
    
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    


  • @masayoshi
    The above looks to me like it should work in principle. So what is the question?

    EDIT
    If you are saying this does not work/causes a problem, https://doc.qt.io/Qt-5/qwebenginepage.html#print

    The settings for creating and printing the PDF document will be retrieved from the printer object. It is the users [sic.] responsibility to ensure the printer remains valid until resultCallback has been called.

    Your QPrinter printDev; goes out of scope at the end of the function. You may need to ensure it does not do so till after the callback is called, so you'll need to do that by whichever means you choose.



  • Thank you.


Log in to reply