开发者

How to embedded the Print Preview Dialog in QT

in the QT, if i use the print preview dialog widget then it's popup as new window. actually i need to show print preview to the user, i need to embedded in my application main w开发者_如何学编程indow. is there any way to do that?. i tried printPreviewWidget but,i did't get the result?

please help me to fix the problem


finally i only found the answer form my question. i hope it will will help to others.

i used QPrintPreviewWidget. please see the code....

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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜