2013-09-30 74 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);