在Qt中打印到纸张

har*_*ash 0 c++ printing qt

我是qt的新手.我想制作一个简单的项目,从打印机打印文本.每当我使用时

   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;
Run Code Online (Sandbox Code Playgroud)

或这个

 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();
Run Code Online (Sandbox Code Playgroud)

我只是将它粘贴到我的mainwindow.cpp(并尝试将其粘贴到main.cpp),以检查它是否有效.它不是.我收到了这样的几个错误

mainwindow.obj:-1:错误:LNK2019:未解析的外部符号"__declspec(dllimport)public:virtual _ thiscall QPrinter :: ~QPrinter(void)"( _ imp _ ?? 1QPrinter @@ UAE @ XZ)在函数"private"中引用: void __thiscall MainWindow :: on_pushButton_clicked(void)"(?on_pushButton_clicked @ MainWindow @@ AAEXXZ).

谁能告诉我一步一步,如何打印到打印机?我也在网上查了很多,但没有得到任何相关的教程,甚至是一个例子.所以,请在这里写,而不是将我链接到另一页.

Seb*_*nge 6

我做了一些快速研究,对你的评论感到惊讶.QtPrintSupport确实发生了变化,因此请使用Qt5(详细说明):

在Pro文件中: QT += core gui printsupport

在cpp文件中: #include <QtPrintSupport>

要从QTextEdit*编辑器打印,请使用:

editor->document()->print(&printer);
Run Code Online (Sandbox Code Playgroud)