Results 1 to 20 of 25

Thread: QTableView printing

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QTableView printing

    Greetings!

    I'm trying to print the contents of a QTableView to the printer (this is my first QT program sending anything to the printer). I didn't know if there was a way as easy as QTextEdit->document()->print(&printer). The closeest I could find was implementing something similar to what I saw in Qt's Pixelator example, but all this seems to do for me is spit out an empty sheet of paper:

    Note: 'model' is previously defined as QAbstractItemModel *model

    Qt Code:
    1. QPrinter printer(QPrinter::HighResolution);
    2.  
    3. QPrintDialog *dlg = new QPrintDialog(&printer, this);
    4. dlg->setWindowTitle(tr("Print Table"));
    5.  
    6. if (dlg->exec() != QDialog::Accepted)
    7. return;
    8.  
    9. QPainter painter;
    10. painter.begin(&printer);
    11.  
    12. int rows = model->rowCount(QModelIndex());
    13. int columns = model->columnCount(QModelIndex());
    14.  
    15. double xscale = printer.pageRect().width();
    16. double yscale = printer.pageRect().height();
    17. double scale = qMin(xscale, yscale);
    18.  
    19. painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
    20. printer.paperRect().y() + printer.pageRect().height()/2);
    21. painter.scale(scale, scale);
    22. painter.save();
    23.  
    24. const int ItemSize = 256;
    25. float x = ItemSize /2;
    26. for (int printRow = 0; printRow < rows; ++printRow) {
    27. float y = ItemSize /2;
    28. for (int column = 0; column < columns; ++column) {
    29. option.rect = QRect(int(x), int(y), ItemSize, ItemSize);
    30. myTableView->itemDelegate()->paint(&painter, option, model->index(printRow, column, parent));
    31. x = x + 256;
    32. }
    33. y = y + 256;
    34. }
    35. painter.restore();
    36. painter.end();
    To copy to clipboard, switch view to plain text mode 

    Any ideas would be greatly appreciated. The only thing I can think of is creating a QTextDocument() and feeding it the QTableView in HTML format and then printing that.

    Thanks,
    --D
    Last edited by wysota; 26th September 2006 at 19:25. Reason: missing [code] tags

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  3. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  4. Replies: 0
    Last Post: 28th June 2006, 20:49
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.