标签:log oid style ctp efi view PDF文件 sel exec
void doPrint(); void doPrintPreview(); void printPreview(QPrinter *printer); void createPdf();
ui->setupUi(this); QAction *action_print = new QAction(tr("打印"),this); QAction *action_printPreview = new QAction(tr("打印预览"),this); QAction *action_pdf = new QAction(tr("生成pdf"),this); connect(action_print,SIGNAL(triggered()),this,SLOT(doPrint())); connect(action_printPreview,SIGNAL(triggered()),this,SLOT(doPrintPreview())); connect(action_pdf,SIGNAL(triggered()),this,SLOT(createPdf())); ui->mainToolBar->addAction(action_print); ui->mainToolBar->addAction(action_printPreview); ui->mainToolBar->addAction(action_pdf);
void MainWindow::doPrint() { // 创建打印机对象 QPrinter printer; // 创建打印对话框 QPrintDialog dlg(&printer, this); //如果编辑器中有选中区域,则打印选中区域 if (ui->textEdit->textCursor().hasSelection()) dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection); // 如果在对话框中按下了打印按钮,则执行打印操作 if (dlg.exec() == QDialog::Accepted) { ui->textEdit->print(&printer); } }
打印预览 void MainWindow::doPrintPreview() { QPrinter printer; // 创建打印预览对话框 QPrintPreviewDialog preview(&printer, this); // 当要生成预览页面时,发射paintRequested()信号 connect(&preview,SIGNAL(paintRequested(QPrinter*)), this,SLOT(printPreview(QPrinter*))); preview.exec(); }
void MainWindow::printPreview(QPrinter *printer) { ui->textEdit->print(printer); }
// 生成PDF文件 void MainWindow::createPdf() { QString fileName = QFileDialog::getSaveFileName(this, tr("导出PDF文件"),QString(), "*.pdf"); if (!fileName.isEmpty()) { // 如果文件后缀为空,则默认使用.pdf if (QFileInfo(fileName).suffix().isEmpty()) fileName.append(".pdf"); QPrinter printer; // 指定输出格式为pdf printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(fileName); ui->textEdit->print(&printer); } }
标签:log oid style ctp efi view PDF文件 sel exec
原文地址:https://www.cnblogs.com/osbreak/p/14342667.html