Results 1 to 7 of 7

Thread: Printing an entire QTreeWidget

  1. #1
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Printing an entire QTreeWidget

    I'm trying to print all of the visible (non-hidden) items in A QTreeWidget. I can successfully print the current viewport, but I'd like to be able to print all of the items in the tree.

    Any suggestions? Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Printing an entire QTreeWidget

    Qt Code:
    1. // Pseudocode, sort of:
    2.  
    3. void printTreeItem( QTreeWidgetItem * pItem, int indentLevel )
    4. {
    5. if ( pItem && !pItem->isHidden() )
    6. {
    7. int nCols = pItem->columnCount();
    8. for ( int nCol = 0; nCol < nCols; ++nCol )
    9. printIndented( pItem->text( nCol ), indentLevel, nCol, nCols );
    10.  
    11. int nKids = pItem->childCount();
    12. for ( int nKid = 0; nKid < nKids; ++nKid )
    13. printTreeItem( child( nKid ), indentLevel + 1 );
    14. }
    15. }
    16.  
    17. void printTree( QTreeWidget * pTree )
    18. {
    19. int nTops = pTree->topLevelItemCount();
    20. for ( int nTop = 0; nTop < nTops; ++nTop )
    21. printTreeItem( pTree->topLevelItem( nTop ), 0 );
    22. }
    To copy to clipboard, switch view to plain text mode 

    I'll let you figure out how to implement this:

    Qt Code:
    1. printIndented( pItem->text( nCol ), indentLevel, nCol, nCols );
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to d_stranz for this useful post:

    bday1223 (14th July 2015)

  4. #3
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Printing an entire QTreeWidget

    Thanks.

    So the idea is to just dump the tree to a text widget? I was kind of just hoping that there would be a convenient way to preserve the formatting, column widths, etc.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Printing an entire QTreeWidget

    How do you currently print?

    Cheers,
    _

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Printing an entire QTreeWidget

    So the idea is to just dump the tree to a text widget?
    No. What I wrote simply recurses through the tree, and for each non-hidden item, extracting its text, "printing" it, then recursing into its non-hidden children. Sort of the tree widget version of pretty-printing.

    If you want to preserve formatting, then your "print" routine is going to have to pull out those other properties - fonts, colors, backgrounds, icons, etc. and "print" them as well. I don't know how you handle column widths - the tree widget's column widths are going to be in screen pixels, not print pixels, so you'll need to figure out how to map that to the equivalent in paper pixels. (In the code I wrote, the "indent level" is equivalent to the column widths - if you determine the column widths in advance and put them into a vector, the indent level is just the index into the vector).

    You might be able to do this with QTextDocument but I think that is going to be a lot harder to implement.

  7. #6
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Printing an entire QTreeWidget

    That's exactly what I ended up doing - creating a QTextDocument, inserting a table, and copying from the tree to the table.
    Thanks again.

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Printing an entire QTreeWidget

    creating a QTextDocument, inserting a table, and copying from the tree to the table
    So long as you don't want any of the tree decorations (eg. lines from top level items to children, etc.) this is a perfectly reasonable way to do it.

Similar Threads

  1. Print entire QTableWidget
    By TonyInSoMD in forum Qt Programming
    Replies: 2
    Last Post: 7th January 2015, 13:02
  2. Replies: 0
    Last Post: 29th April 2013, 11:47
  3. QCryptographicHash an entire file
    By pyramation in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2010, 23:41
  4. Drawing the entire window at 60 fps
    By antiquechrono in forum Qt Programming
    Replies: 1
    Last Post: 25th February 2010, 20:10
  5. Accessing the entire file..
    By Kapil in forum Newbie
    Replies: 1
    Last Post: 28th April 2006, 07:41

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.