简体   繁体   中英

Print function using Qt

I will make this fairly quick as it's a very simple question. How do I make a slot in Qt that prints out everything inside of my text editor. Simply a print button that behaves as you'd expect a print button to behave. I'm reading about a QPrintDialog, but I can't get it to work. This is what I tried:

QPrintDialog printDialog(printer, this);
if(printDialog.exec() == QDialog::Accepted){
    ui->textBox->print(printer);
}

I tried doing QPrinter *printer = new QPrinter before it... but that gave me all sorts of errors, which I kind of figured it would. I already have #include QPrintDialog so how do I actually get this to work? Any help or advice is much appreciated.

Edit: I was looking for an answer for my question and I found a code that seems to be closer to working if that makes sense.

QPrinter printer;

QPrintDialog *printDialog = new QPrintDialog(&printer, this);
printDialog->setWindowTitle("Print Document");
if (printDialog->exec() != QDialog::Accepted)
         return;

However, now I get an error that says:

C:\Documents and Settings\Me\My Documents\C++ Projects\Qt Gui Applications\WordWrite-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\WordWrite\wordwritemain.cpp:130: error: aggregate 'QPrinter printer' has incomplete type and cannot be defined

I solved the answer to the question on the help part of the Nokia website. the code I used was:

QPrinter printer;
QPrintDialog *printDialog = new QPrintDialog(&printer, this);
printDialog->setWindowTitle("Print Document");
if (printDialog->exec() != QDialog::Accepted){
    ui->textBox->print(&printer);
}

Which is basically what I was doing before except:

  • I added &printer instead of printer

- I added #include <QPrinter>

Thanks for the help anyway, and I hope this helps future text editor developers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM