在Qt中打印预览

时间:2015-03-28 11:31:29

标签: c++ qt

我想要预览页面或想要打印预览。预览窗口正在打开,但qt窗口的内容未在其中。我打印预览的代码如下:

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

如何获取qt窗口的内容。我做错了吗?请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

如果您要打印对话框或窗口小部件的内容,则print广告位应如下:

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