How to print/export a QTreeWidget content?



  • Assuming I have a QTreeWidget like that

    !http://i.stack.imgur.com/A1jUW.png(Print QTreeWidget)!

    I tried this code to Print/Export this QTreeWidget to a file (The name of this QTreeWidget is trvListVehicle)

    @
    QPrinter printer(QPrinter::HighResolution);
    QPainter painter;
    painter.begin(&printer);

    double xscale = printer.pageRect().width() / double(ui->trvListVehicle->width());
    double yscale = printer.pageRect().height() / double(ui->trvListVehicle->height());
    double scale = qMin(xscale, yscale);
    painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
    printer.paperRect().y() + printer.pageRect().height()/2);
    painter.scale(scale, scale);
    painter.translate(-width()/2, -height()/2);

    ui->trvListVehicle->render(&painter);
    @

    The program shows Print dialog for me to choose printer machine. After printing completely, I got a blank page. The page didn't contain any content.

    Could you help me?

    Thanks!



  • Never used the QPrinter myself before, but if I read the doc correct you should use the QPainter::begin() function to prepare the printer for writing to the page. When a new page is needed a QPainter::newPage().
    That might help for the actual printing.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.