簡體   English   中英

使用Qt Creator在C ++中打印文本

[英]Print the text in C++ using Qt creator

使用C ++作為Qt Creator中的語言,我創建了一個記事本(與Microsoft Windows的簡單文本編輯器相同,它是一個基本的文本編輯程序),但是我找不到用於打印選項的確切代碼另存為圖像或pdf文件並打印在創建的記事本中寫入的內容。 書面代碼給出錯誤提示

...\NotePad\mainwindow.cpp:5: error: QPrinter: No such file or directory
 #include <QPrinter>

編寫代碼

#include <QPrinter>
void MainWindow::on_actionPrint_triggered()
{
    QPrinter printer(QPrinter::HighResolution);
    printer.setOutputFileName("print.ps");
    painter.end();
}

您可以將QTextDocument用於類似這樣的簡單打印任務。 假設您已將文本加載到其中,則可以執行以下操作(我以使用pdf打印為例,可以在任何位置打印):

QTextDocument doc; // your text is here
QPrinter printer;
printer.setOutputFileName("<your_file_name_goes_here");
printer.setOutputFormat(QPrinter::PdfFormat);
doc.print(&printer);
printer.newPage(); // this might not be necessary if you want just 1 page, I'm not sure

如果要使用QPainter,則應

QPrinter printer;
// setup the printer
QPainter painter;

if(!painter.begin(&printer))
{
   // return, throw exception, whatever
}
painter.drawText(10, 10, "your_text");
printer.newPage(); // Again, this might not be necessary if you want just 1 page, I'm not sure
painter.end();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM