Print multiple page with Qprinter and QML form



  • 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
    

  • Lifetime Qt Champion

    Hi,

    Are you using the code from that solved thread ?



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.