我正在尝试通过QWebView创建报告,通过QPrintPreviewDialog显示并打印报告。假设我要创建一个100行的表格,该表格分为几页,然后将当前行号添加到每页的页脚中(我的实际任务的抽象变体)。我的代码:
void MainWindow::preview(){
QPrinter printer;
printer.setPageSize(QPrinter::A4);
printer.setOrientation(QPrinter::Portrait);
printer.setPageMargins(10,10,10,10,QPrinter::Millimeter);
QPrintPreviewDialog print_preview(&printer, this);
print_preview.setWindowState(Qt::WindowMaximized);
connect(&print_preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(paint_pages(QPrinter*)));
print_preview.exec();
}
void MainWindow::paint_pages(QPrinter *printer){
QList<QWebView*> pages;
QWebView *current = 0;
QPainter painter(printer);
int i = 0;
while(i <= 100){
current = new QWebView();
pages << current;
i = populate_web(current, printer, i);
}
int pc = pages.count();
for(i = 0; i < pc; i++){
if(i != 0) printer->newPage();
pages.at(i)->render(&painter);
}
for(i = 0; i < pc; i++)
delete pages.at(i);
}
int MainWindow::populate_web(QWebView *pg, QPrinter *printer, int n){
QString html = "<html><body>";
html += "<table cellspacing=0 border = 1 style='border-collapse: collapse'>";
int page_height = printer->paperRect(QPrinter::Point).height();
for(++n; n <= 100; n++){
html += QString("<tr><td width=200>%1</td><td width=200>%2</td><td width=300>%3</td></tr>").arg(n).arg(n*n).arg(n*n*n);
QString html2 = html + "</table></body></html>";
pg->setHtml(html2);
int content_height = pg->page()->mainFrame()->contentsSize().height();
if(content_height + 20 > page_height){
html += "</table>";
html += QString("<p>Current value: %1</p>").arg(n);
break;
}
}
if(n > 100) html += "</table>";
html += "</body></html>";
pg->setHtml(html);
return n;
}
因此,我希望将 table 放在整张纸上,除了10毫米的边距。但是相反,我得到了一些奇怪的东西(PICTURE HERE);
更重要的是-滚动条从第二页开始就没有出现在首页上。我要怎么做才能用表格填满整个页面并从滚动条释放页面?
最佳答案
下一行:
html += "</body></html>";
添加此行:
pg->setFixedSize(QSize(printer->width(),printer->height()));
关于c++ - QWebView : print problems,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15635706/