简体   繁体   中英

Print Preview in Qt

I want to preview a page or want to have print preview. A preview window is opening but the contents of qt window is not comming in it. My code to print preview is as follows:

void User::on_actionprintPreview_triggered()
{
    QPrinter printer(QPrinter::HighResolution);
    QPrintPreviewDialog preview(&printer, this);
    connect(&preview, SIGNAL(paintRequested(QPrinter *)),
            this, SLOT(print(QPrinter *)));
    preview.exec();
}

void User::print(QPrinter *printer)
{
    // print the page
    QPainter painter(printer);
    int w = printer->pageRect().width();
    int h = printer->pageRect().height();
    QRect page(0, 0, w, h);

    QFont font = painter.font();
    font.setPixelSize((w+h)/100);
    painter.setFont(font);

    painter.drawText(page, Qt::AlignBottom | Qt::AlignRight,
                     QDateTime::currentDateTime().
                     toString(Qt::DefaultLocaleShortDate));

    page.adjust(w/20, h/20, -w/20, -h/20);
}

How can I get the content of the qt window in it. Does I am doing something wrong. Please help me out to solve this.

If you want to print the contents of your dialog or widget, your print slot should be like:

void User::print(QPrinter * printer)
{
    QPainter painter(printer);
    painter.setWindow(this->rect());
    this->render(&painter);
}

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