Results 1 to 6 of 6

Thread: Scroll-to-column in QTreeWidget?

  1. #1
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Scroll-to-column in QTreeWidget?

    I have a QTreeWidget displaying a hierarchy of nodes. I'm successfully using QTreeWidget::scrollToItem() to make sure a given item is visible, however sometimes the item is so far on the right (being so deep in the hierarchy) that one needs to scroll the tree widget horizontally to make it actually appear. Is there a way (or a trick) to make a given column (0 in my case) of a given item visible, a scrollToColumn() kind of thing?
    Last edited by dictoon; 21st May 2013 at 09:46.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Scroll-to-column in QTreeWidget?

    Is there a way (or a trick) to make a given column (0 in my case) of a given item visible, a scrollToColumn() kind of thing?
    You can use QTreeView::scrollTo(const QModelIndex & index, ScrollHint hint = EnsureVisible);
    Example:
    Qt Code:
    1. // Get the index of the item (ITEM-A) on 10th row 1st column
    2. QModelIndex index = mTreeWidget->model()->index(9, 0); // Row 10, Column 1
    3.  
    4. // Get the index of the child of ITEM-A on Row 1, and 10th column.
    5. index = index.child(0, 9); // Row 1, Column 10
    6.  
    7. // Scroll to item
    8. mTreeWidget->scrollTo(index);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scroll-to-column in QTreeWidget?

    Hey Santosh,

    Thanks for your reply. I'm afraid I couldn't make it to work though. Here is my implementation based on your suggestion:

    Qt Code:
    1. void compute_index(QTreeWidget* tree, QTreeWidgetItem* item, QModelIndex& index)
    2. {
    3. if (item->parent())
    4. {
    5. compute_index(tree, item->parent(), index);
    6. index = index.child(item->parent()->indexOfChild(item), 0);
    7. }
    8. else
    9. {
    10. index = tree->model()->index(tree->indexOfTopLevelItem(item), 0);
    11. }
    12. }
    13.  
    14. // Later, given item and treeWidget:
    15.  
    16. compute_index(treeWidget, item, index);
    17. treeWidget->scrollTo(index);
    To copy to clipboard, switch view to plain text mode 

    The result is exactly the same as what I get using QTreeWidget::scrollToItem(): no horizontal scrolling is done to ensure the item is actually visible.

    Franz

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Scroll-to-column in QTreeWidget?

    Try this, I have checked it works just fine.
    Qt Code:
    1. QModelIndex computeModelIndex(QTreeWidgetItem * item, int column = 0)
    2. {
    3. QModelIndex index;
    4.  
    5. if(item == 0)
    6. return index;
    7.  
    8. QTreeWidget * treeWidget = item->treeWidget();
    9.  
    10. if(treeWidget == 0)
    11. return index;
    12.  
    13. QTreeWidgetItem * parent = item->parent();
    14.  
    15. if(parent)
    16. return computeModelIndex(parent, 0).child(parent->indexOfChild(item), column);
    17.  
    18. return treeWidget->model()->index(treeWidget->indexOfTopLevelItem(item), 0);
    19. }
    20.  
    21. //elsewhere
    22. QModelIndex index = computeModelIndex(item, 9);
    23. if(index.isValid())
    24. mTreeWidget->scrollTo(index);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    dictoon (23rd May 2013)

  6. #5
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scroll-to-column in QTreeWidget?

    Hey Santosh,

    Thanks again for taking the time to answer with detailed code.

    I'm afraid I wasn't very clear on my problem, so I created a small test application (Windows, x64) that illustrates well the issue I'm trying to solve:




    Start the application, then press the "Select Random Item" button at the bottom to select a random item in the tree widget. As you will notice, although the tree widget scrolls horizontally to the selected item, the actual text of the item isn't visible as it's too far on the right (due to the indentation). What I'm trying to do is to always make the text of the item visible.

    (Note that, although the tree widget has two columns, only the first one (at index 0) is populated and this is the one I would like to make visible.)

    Franz

  7. #6
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scroll-to-column in QTreeWidget?

    I solved my problem by simply doing the following:

    Qt Code:
    1. ui->treeWidget->scrollToItem(item);
    2. const QRect r = ui->treeWidget->visualItemRect(item);
    3. ui->treeWidget->horizontalScrollBar()->setValue(r.x());
    To copy to clipboard, switch view to plain text mode 

    Basically, I retrieve the horizontal position of the item within the viewport of the tree widget, then I adjust the horizontal scrollbar such that it aligns with the left of the item.

    Thanks again Santosh for your time.

    Franz

Similar Threads

  1. Replies: 5
    Last Post: 10th October 2011, 13:26
  2. QtreeWidget horizon scroll
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 20th April 2009, 10:00
  3. QTreeWidget scroll only one column
    By nina1983 in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2008, 15:07
  4. QTreeWidget won't scroll horizontally
    By JimDaniel in forum Qt Programming
    Replies: 3
    Last Post: 23rd January 2008, 08:53
  5. set Column Width in QTreeWidget?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 08:12

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.