,如果我使用打印预览对话框小部件,那么它会弹出为新窗口。实际上我需要向用户显示打印预览,我需要嵌入我的应用程序主窗口。有没有办法做到这一点?我试过printPreviewWidget但是,我没有得到结果?
请帮我解决问题
答案 0 :(得分:6)
最后我只是从我的问题中找到答案。我希望它会对其他人有所帮助。
我使用了QPrintPreviewWidget。请看代码......
Widget::Widget(QWidget *parent, QWidget *report) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
printer = new QPrinter(QPrinter::PrinterResolution);
printer->setOutputFormat(QPrinter::PdfFormat);
printer->setOutputFileName("sample.pdf");
printer->setPaperSize(QPrinter::A4);
printer->setFullPage(true);
printer->setResolution(300);
preview = new QPrintPreviewWidget(printer, this);
ui->verticalLayout->addWidget(preview);
preview->setFont(QFont("Arial",18,QFont::Bold));
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(print()));
connect(preview, SIGNAL(paintRequested(QPrinter*)), report, SLOT(Print(QPrinter*)));
preview->setZoomFactor(1);
preview->show();
}
void Widget::print()
{
preview->print();
}
void Report::Print(QPrinter *printer)
{
QPainter p(printer);
p.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true);
for (int page = 0; page < 5; page++)
{
p.drawText(1750, 200, QString("Date: %1").arg(QDateTime::currentDateTime().toString("dd.MM.yyyy")));
p->fillRect(QRect(150,150,2179,125),QBrush(QColor(222,222,230,255)));
printer->newPage();
}
}