-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintFirst.cpp
34 lines (29 loc) · 935 Bytes
/
PrintFirst.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "PrintFirst.h"
#include <QDebug>
#include <QPainter>
#include <QPrintDialog>
#include <QPrinter>
PrintFirst::PrintFirst(QObject *parent)
: QObject(parent), _printer(new QPrinter) {
if (_printer->isValid()) {
qDebug() << "printer name:" << _printer->printerName()
<< "\t,state:" << _printer->printerState();
} else {
qDebug() << "Printer is not valid!";
}
}
static bool showPrinterDialog(QPrinter *printer) {
QScopedPointer<QPrintDialog> dialog(new QPrintDialog(printer));
dialog->setWindowTitle("Print Document");
return dialog->exec() == QDialog::Accepted;
}
bool PrintFirst::print(const QString &text) {
if (!_printer->isValid()) return false;
if (!showPrinterDialog(_printer)) return false;
QPainter painter;
if (painter.begin(_printer)) {
painter.drawText(100, 100, 500, 500, Qt::AlignLeft | Qt::AlignTop, text);
return painter.end();
} else
return false;
}