Search code examples
qtprintingqmlqtquick2qt-quick

How to print(with the printer) a QML object?


I have designed a sales receipt with Qt Quick and I want to print it with the printer.

How can I do this?

Here is my main.cpp

QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/Caisse-MBM/main.qml"));
viewer.showFullScreen();

Solution

  • You can use QQuickView::grabWindow() to get a QImage and then do whatever you want with it, print it, save it...

    QImage image = view->grabWindow();
    

    Afterwards you can follow this post to get the image to print.