[英]Printing to paper in Qt
I am new to qt. 我是qt新手。 I want to make a simple project that will print text from the printer. 我想做一个简单的项目,将打印打印机中的文本。 whenever I am using 每当我使用
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer, this);
dialog->setWindowTitle(tr("Print Document"));
if (editor->textCursor().hasSelection())
dialog->addEnabledOption(QAbstractPrintDialog::PrintSelection);
if (dialog->exec() != QDialog::Accepted)
return;
or this 或这个
QPrinter printer(QPrinter::HighResolution);
printer.setOutputFileName("print.ps");
QPainter painter;
painter.begin(&printer);
for (int page = 0; page < numberOfPages; ++page) {
// Use the painter to draw on the page.
if (page != lastPage)
printer.newPage();
}
painter.end();
I just copy pasted this to my mainwindow.cpp(and tried pasting it to main.cpp too),to check if it works. 我只是将其复制粘贴到我的mainwindow.cpp(并尝试将其粘贴到main.cpp),以检查它是否有效。 It does not. 它不是。 I am getting several errors like these 我收到类似这样的几个错误
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual _ thiscall QPrinter::~QPrinter(void)" ( _imp_??1QPrinter@@UAE@XZ) referenced in function "private: void __thiscall MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked@MainWindow@@AAEXXZ). mainwindow.obj:-1:错误:LNK2019:未解析的外部符号“ __declspec(dllimport)公共:虚拟_ thiscall QPrinter :: 〜QPrinter (void)”( _imp _ ?? 1QPrinter @@ UAE @ XZ)在函数“私人: void __thiscall MainWindow :: on_pushButton_clicked(void)“(?on_pushButton_clicked @ MainWindow @@ AAEXXZ)。
could someone tell me step by step, how to print to a printer? 有人可以一步一步告诉我,如何打印到打印机吗? I also checked a lot online, but did not get any relevant tutorial , or even an example. 我也在网上检查了很多,但没有得到任何相关的教程,甚至没有示例。 so, PLEASE write it here instead of linking me to another page. 因此,请在此处写下而不是将我链接到另一页。
I did some quick research being kind of surprised by your comments. 我进行了一些快速研究,使您的评论感到惊讶。 The QtPrintSupport did change, so use for Qt5 ( Detailed Description ): QtPrintSupport确实发生了变化,因此用于Qt5( 详细说明 ):
In Pro file: QT += core gui printsupport
在Pro文件中: QT += core gui printsupport
In cpp file: #include <QtPrintSupport>
在cpp文件中: #include <QtPrintSupport>
For printing from your QTextEdit *editor use: 要从QTextEdit * editor打印,请使用:
editor->document()->print(&printer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.