2013-09-30 76 views
0

我是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; 

或本

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(); 

我只是复制粘贴此我mainwindow.cpp(并试图将其粘贴到main.cpp中太),以检查是否正常工作。它不是。 我越来越喜欢这些

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual _thiscall QPrinter::~QPrinter(void)" ([email protected]@[email protected]) referenced in function "private: void __thiscall MainWindow::on_pushButton_clicked(void)" ([email protected]@@AAEXXZ).

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

+1

听起来像你没有正确配置你的项目(Visual C++,我假设?)。您需要在项目设置中添加Qt库,以便链接程序能够找到它们。 – us2012

+0

您可能错过了#include 声明? –

+0

每当我想包括,它是建议 我正在使用qtCreator和msvc2010 这里是一个快照: http://i39.tinypic.com/2h82r74.jpg –

回答

6

我做了一些快速调查,感到很惊讶你的意见。该QtPrintSupport的确发生了变化,所以使用QT5(Detailed Description):

在Pro文件: QT += core gui printsupport

在CPP文件: #include <QtPrintSupport>

有关从您的QTextEdit *编辑器使用的印:

editor->document()->print(&printer);