Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ui->webEngineView->page()->print...

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

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 446 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    masayoshi
    wrote on last edited by masayoshi
    #1

    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
    
    
    JonBJ 1 Reply Last reply
    0
    • M masayoshi

      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
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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.

      1 Reply Last reply
      2
      • M Offline
        M Offline
        masayoshi
        wrote on last edited by
        #3

        Thank you.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved