Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Print multiple page with Qprinter and QML form
Forum Updated to NodeBB v4.3 + New Features

Print multiple page with Qprinter and QML form

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 1.1k Views 2 Watching
  • 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.
  • F Offline
    F Offline
    filipdns
    wrote on last edited by
    #1

    Hello,

    I'm trying for the first time to use Qpainter and Qprinter and when I'm printing, what is outside of the screen is cut and only one page is printed, how can I have print multiple page in pdf.

    Other problem, I probably did mistake but with my first qml with listview, I was enable to scroll to see what was out off the screen but now it's not working anymore...

    Thank you very much for your help

    here the qml for for test:

    import QtQuick 2.9
    import QtQuick.Layouts 1.3
    import QtQuick.Controls 2.1
    import QtQuick.Controls.Styles 1.1
    import QtQuick.Extras 1.4
    import QtQuick.Controls 1.4
    import Qt3D.Extras 2.0
    import QtQuick.LocalStorage 2.0
    import QtQuick.Window 2.3
    
    
    Page {
        width: 1280
        height: 1024
        visible: true
        header: ToolBar {
            width: parent.width
            height: 60
            style: ToolBarStyle {
                background: Rectangle {
                    gradient: Gradient {
                        GradientStop { position: 0 ; color: "black" }
                        GradientStop { position: 1 ; color: "green" }
                    }
                }
            }
            Rectangle {
                id: backButton
                width: 60
                anchors.left: parent.left
                anchors.leftMargin: 25
                anchors.verticalCenter: parent.verticalCenter
                antialiasing: true
                height: 60
                radius: 4
                color: backmouse.pressed ? "darkgreen" : "transparent"
                Behavior on opacity { NumberAnimation{} }
                Image {
                    anchors.verticalCenter: parent.verticalCenter
                    source: "images/navigation_previous_item.png"
                }
                MouseArea {
                    id: backmouse
                    anchors.fill: parent
                    //anchors.margins: -10
                    onClicked: stackView.pop(null)
                }
            }
    
            Label {
                id: pageTitle
                text: "Présentation Print"
                textFormat: Text.AutoText
                fontSizeMode: Text.VerticalFit
                font.pixelSize: 50
                color: "lightgrey"
                anchors.centerIn: parent
                font.family: "Times New Roman"
                font.bold: true
                font.italic: true
    
            }
        }
        Rectangle {
            id: pageRect
            color: 'white'
            width: parent.width // When one sets a size using the 'setPageSize' method, this changes.
            height: parent.height // When one sets a size using the 'setPageSize' method, this changes.
            //visible: false // Can be true or false, still works!
            border.color: "black"
            border.width: 2
    
    
                ListView {
                    id: listView
                    //Layout.fillWidth: true
                    model: ListModel {
                        id: listModel
                        Component.onCompleted:
                        {
                            JS.dbpresentation("F-GALP")
    
                        }
                    }
                    delegate: Item {
                        width: parent.width
                        height: parent.height
    
    
                        GridLayout {
                            id: gridLayout
                            columns: 4
                            flow: GridLayout.TopToBottom
                            //anchors.fill: parent
                            columnSpacing: Screen.width / 30
    
                            Text {
                                text: "Immatriculation"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Type"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero de serie"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de construction"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numéro du certificat d'immatriculation"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero de la LSA"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                id:datelsa
                                text: "Date de validité de la LSA"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero du certificat de navigabilité"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité du CDN"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité du certificat d'assurance"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité de la pesée"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Marque Moteur"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Model Moteur"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Marque Helice"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Model Helice"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Moteur 1"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Moteur 2"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
    
                            Text {
                                text: "SN Helice 1"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Helice 2"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
    
                            Text {
                                text: "Immatriculation"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Type"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero de serie"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de construction"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numéro du certificat d'immatriculation"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero de la LSA"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité de la LSA"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero du certificat de navigabilité"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité du CDN"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité du certificat d'assurance"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité de la pesée"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Marque Moteur"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Model Moteur"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Marque Helice"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Model Helice"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Moteur 1"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Moteur 2"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
    
                            Text {
                                text: "SN Helice 1"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Helice 2"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
    
                            Text {
                                text: "Heure Cellule"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Cycle Cellule"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Heure Moteur 1 SNEW"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Heure Moteur 1 SOVL"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Heure Moteur 1 SHSI"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Cycle Moteur 1 SNEW"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Cycle Moteur 1 SOVL"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Cycle Moteur 1 SHSI"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Heure Moteur 2 SNEW"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Heure Moteur 2 SOVL"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Heure Moteur 2 SHSI"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Cycle Moteur 2 SNEW"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Cycle Moteur 2 SOVL"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Cycle Moteur 2 SHSI"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Heure Helice 1 SOVL"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Cycle Helice 1 SOVL"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Heure Helice 2 SOVL"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Cycle Helice 2 SOVL"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Immatriculation"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Type"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero de serie"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de construction"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numéro du certificat d'immatriculation"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero de la LSA"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité de la LSA"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero du certificat de navigabilité"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité du CDN"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité du certificat d'assurance"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité de la pesée"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Marque Moteur"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Model Moteur"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Marque Helice"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Model Helice"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Moteur 1"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Moteur 2"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
    
                            Text {
                                text: "SN Helice 1"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Helice 2"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Immatriculation"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Type"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero de serie"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de construction"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numéro du certificat d'immatriculation"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero de la LSA"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
    
                                text: "Date de validité de la LSA"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero du certificat de navigabilité"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité du CDN"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité du certificat d'assurance"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité de la pesée"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Marque Moteur"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Model Moteur"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Marque Helice"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Model Helice"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Moteur 1"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Moteur 2"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
    
                            Text {
                                text: "SN Helice 1"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Helice 2"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
    
                            Text {
                                text: "Immatriculation"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Type"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero de serie"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de construction"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numéro du certificat d'immatriculation"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero de la LSA"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité de la LSA"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Numero du certificat de navigabilité"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité du CDN"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité du certificat d'assurance"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Date de validité de la pesée"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Marque Moteur"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Model Moteur"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Marque Helice"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "Model Helice"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Moteur 1"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Moteur 2"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
    
                            Text {
                                text: "SN Helice 1"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                            Text {
                                text: "SN Helice 2"
                                font.pixelSize: 20
                                rightPadding: 10
                            }
                        }
                    }
                }
    
        }
    
        Button {
            id: button1
            x: 437
            y: 137
            width: 150
            height: 36
            text: qsTr("Print")
            onClicked: {
                var stat = pageRect.grabToImage(function(result) {
                    //result.saveToFile("/home/user/someimage.png"); //saves to a file
                    PRINT.print(result.image); //result.image holds the QVariant
                });
                console.log("Success: ", stat);
            }
        }
    
    }
    
    

    here the main.cpp

    
    #include <QtWidgets>
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QtWebEngine/QtWebEngine>
    #include <pr.h>
    
    int main(int argc, char *argv[])
    {
        QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        QtWebEngine::initialize();
    
    
        engine.setOfflineStoragePath(".");
        pr  print;
        engine.rootContext()->setContextProperty("PRINT", &print);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        return app.exec();
    }
    
    

    here pr.cpp

    #include "pr.h"
    #include <QPrinter>
    #include <QPainter>
    #include <QPrintDialog>
    #include <QPixmap>
    #include <QImage>
    #include <qDebug>
    
    pr::pr()
    {
    
    }
    
    void pr::print(QVariant data)
    
    {
    
    
        QImage img = qvariant_cast<QImage>(data);
        QPrinter printer(QPrinter::ScreenResolution);//The QPrinter class is a paint device that paints on a printer
        //printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOrientation(QPrinter::Portrait);
        printer.setPageSize(QPrinter::A4);
              QPrintDialog *dlg = new QPrintDialog(&printer,0);
              if(dlg->exec() == QDialog::Accepted) {
                      QPainter painter(&printer);
                      QPainter::Antialiasing;
                      QPainter::TextAntialiasing;
                      painter.drawImage(QPoint(0,0),img);
                      painter.end();
              }
    
    
    }
    

    here the pr.h

    #ifndef PR_H
    #define PR_H
    
    #include <QObject>
    #include <QVariant>
    
    class pr : public QObject
    {
        Q_OBJECT
    
    public:
        pr();
    
    public:
    
    Q_INVOKABLE void print(QVariant data);
    
    
    };
    
    #endif // PR_H
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you using the code from that solved thread ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Are you using the code from that solved thread ?

        F Offline
        F Offline
        filipdns
        wrote on last edited by filipdns
        #3

        @SGaist Hello, yes I used this code and it's working to print the screen but not multiple page

        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