Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 757
SlimIT Avatar asked May 28 '13 12:05

SlimIT


People also ask

How do I print a value in QML?

QML doesnot have any such mechanism of printing. You will need to make use of Qt's C++ API's for actual printing. First you can grab image of any Item using grabToImage the result will be delegated to a callback. Inside it you can get the grabbed image data which is actually a QImage .

Does qt print?

Qt provides extensive cross-platform support for printing. Using the printing systems on each platform, Qt applications can print to attached printers and across networks to remote printers. Qt's printing system also supports PDF file generation, providing the foundation for basic report generation facilities.


1 Answers

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.

like image 128
dtech Avatar answered Oct 11 '22 01:10

dtech