Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QWebView: print problems

Tags:

c++

qt

I'm trying to create report via QWebView, show it via QPrintPreviewDialog and print it. Suppose I want to create the 100-rows table splitted onto several pages and add current row number to the footer of each page (abstract variant of my real task). My code:

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

So, I hope to get the table on the whole paper rect, except of 10-millimeter margins. But instead of it, I get something strange (PICTURE HERE); What's more - scrollbar not appears on the first page, just since second. What have I to do to fill whole page with my table and release pages from scrollbars?

like image 940
Kirill Yurchenko Avatar asked Nov 13 '22 08:11

Kirill Yurchenko


1 Answers

After line:

html += "</body></html>";

Add this line:

pg->setFixedSize(QSize(printer->width(),printer->height()));
like image 171
user2524405 Avatar answered Nov 15 '22 04:11

user2524405