harveyslash
harveyslash

Reputation: 6012

Printing to paper in Qt

I am new to 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. 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).

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.

Upvotes: 0

Views: 2661

Answers (1)

Sebastian Lange
Sebastian Lange

Reputation: 4029

I did some quick research being kind of surprised by your comments. The QtPrintSupport did change, so use for Qt5 (Detailed Description):

In Pro file: QT += core gui printsupport

In cpp file: #include <QtPrintSupport>

For printing from your QTextEdit *editor use:

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

Upvotes: 6

Related Questions