Results 1 to 15 of 15

Thread: How to get scrollbars in QTreeWidget

  1. #1
    Join Date
    Jan 2006
    Location
    Leiden, the Netherlands
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to get scrollbars in QTreeWidget

    Hello,

    I use a QTreeWidget (target platform Win32) and set the horizontal scroll bar policy to to Qt::ScrollBarAsNeeded. However I never get a scrollbar.

    Does anyone know how to fix this?

    Arthur

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get scrollbars in QTreeWidget

    Maybe you never need it?

    Can you provide a minimal compilable example which reproduces the problem?

  3. #3
    Join Date
    Jan 2006
    Location
    Leiden, the Netherlands
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get scrollbars in QTreeWidget

    Here it is
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get scrollbars in QTreeWidget

    maybe we do need it!!! I got exactly the same problem with DevQt project manager (all those who tested may have noticed) and I didn't find any way to fix it! Looks like a Qt issue (maybe only on M$ Win but still a Qt issue)...
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get scrollbars in QTreeWidget

    Quote Originally Posted by Arthur
    Here it is
    This is not a minimal compilable example. One can hardly to anything with a complex, yet not using layouts window.

  6. #6
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get scrollbars in QTreeWidget

    I comment how I solve this problem: I declare one additional column at the end of the tree widget and I let it empty (it's a bit ugly but in this way always appears the scroll bar when is needed and also if you hide the header it's not noticed )
    Last edited by SkripT; 14th April 2006 at 17:25.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get scrollbars in QTreeWidget

    It should be achievable if you resize the column. Either by calling resizeColumnToContents (or something like that) or QHeaderView::resizeSection(). One of them will probably fix the issue. Qt behaviour is fine -- it is not the view contents which doesn't fit on the viewport (which would mean a scrollbar has to be added), it is the column which is too narrow to fit contents.

  8. #8
    Join Date
    Jan 2006
    Location
    Leiden, the Netherlands
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get scrollbars in QTreeWidget

    Quote Originally Posted by wysota
    This is not a minimal compilable example. One can hardly to anything with a complex, yet not using layouts window.
    Well, this is exactly nothing more than a simple project containing the widget layout where the problem occurs. If I just put a treeview on a form, it will probably work...

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get scrollbars in QTreeWidget

    Quote Originally Posted by Arthur
    Well, this is exactly nothing more than a simple project containing the widget layout where the problem occurs. If I just put a treeview on a form, it will probably work...
    There is no layout there. Try resizing the window. The form (and other widgets too) is completely unnecessary, it would be better if there was only the treewidget there:

    Qt Code:
    1. #include <QApplication>
    2. #include <QTreeWidget>
    3. #include <QStringList>
    4.  
    5. int main(int argc, char **argv){
    6. QApplication app(argc, argv);
    7. // ... some lines here, like:
    8. tw.insertTopLevelItem(0,
    9. new QTreeWidgetItem( QStringList() << "col1" << "col2" )
    10. );
    11. tw.show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    That's a minimal compilable example

  10. #10
    Join Date
    Apr 2006
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to get scrollbars in QTreeWidget

    I hope I can help you by providing you the following example code.

    class MyTreeWidget: public QTreeWidget
    {
    Q_OBJECT
    public:
    MyTreeWidget(QWidget* parent = 0): QTreeWidget(parent)
    {
    setObjectName(QString::fromUtf8("objectView"));
    setGeometry(QRect(10, 20, 120, 150));
    setColumnCount(2);
    header()->resizeSection(0, 0);
    setItemHidden(headerItem(), true);

    QObject::connect(this, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(updateColumnWidth()));
    QObject::connect(this, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(updateColumnWidth()));
    QObject::connect(this, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(updateColumnWidth()));
    }

    protected slots:
    void updateColumnWidth()
    {
    header()->resizeSection(0, 0);
    resizeColumnToContents(0);
    }
    };

    To make your tree widget support horizontal scroll bar and resize automatically to fit the width of the contents that are displaying, you may create your own tree widget class derived from QTreeWidget, and "make it work well" at the constructor.
    Take a look at the highlighted code, we have to make it have at least two columns (just as SkripT said) by adding setColumnCount(2), and initialize the width of column 0 to 0 (or the scroll bar will appear even it contains nothing).
    Then, to make it resize automatically when the content displaying on the widget has changed, we add the function updateColumnWidth() as a slot, which is connected by three signals: itemChanged, itemExpanded and itemCollapsed, since the content displayed on the widget may be changed when one of the signals is emitted.

    And in function updateColumnWidth(),
    header()->resizeSection(0, 0);
    resizeColumnToContents(0);
    are called sequentially. The function resizeColumnToContents(column) resizes the column to fit the contents that are displaying, but it will only grow when the width increases (will not shrink when the width decreases), therefore we have to reset the width of the column and stretch it to fit the contents.

    Hope it helps you. Please reply me if any questions.
    Sincerely yours,
    minorpan

  11. The following user says thank you to minorpan for this useful post:

    Arthur (1st May 2006)

  12. #11
    Join Date
    May 2006
    Location
    Finland
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get scrollbars in QTreeWidget

    Thank you minorpan, your advice was excellent!

    However, I have one more question:
    with your advice resizing works automatically OK otherwise, but not in case the window is resized (the user drags with her mouse from the corner of the window and enlarges or shrinks it). When I try to cover also that by re-defining the event-handling for the appropriate event:

    void MyTreeWidget::resizeEvent( QResizeEvent* event)
    {
    QTreeWidget::resizeEvent( event);

    // in addition to plain old resizing update the column widths:
    updateColumnWidth(); // minorpan's function
    };


    it works... almost. With my application I am always able to find such a tree expansion/window size combination, that after the window has been resized with the mouse, the scollbars flick enourmously and the PC CPU usage is close to 100%. How should I implement the resize-support properly?
    - Jorma

  13. #12
    Join Date
    May 2006
    Location
    Finland
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get scrollbars in QTreeWidget

    I'll answer myself now that I found out how to do it...

    in order to get everything right, it is enough to say in MyTreeWidget's constructor also

    setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn);
    setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn);

    connect(this->horizontalScrollBar(), SIGNAL(actionTriggered(int)),
    this, SLOT(updateColumnWidth()));
    connect(this->verticalScrollBar(), SIGNAL(actionTriggered(int)),
    this, SLOT(updateColumnWidth()));

  14. #13
    Join Date
    Apr 2006
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to get scrollbars in QTreeWidget

    Dear Jorma:

    I have tried to reconstruct your problem by adding the following code to function main().

    #include "MyTreeWidget.h"
    int main(int argc, char* argv[])
    QApplication app(argc, argv);
    MyTreeWidget widget;
    QTreeWidgetItem* pItem;
    pItem = new QTreeWidgetItem(&widget, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(&widget, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(&widget, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    pItem = new QTreeWidgetItem(pItem, QStringList("aaaaaaaaaaaaaaaaaaaaaaa"));
    widget.show();
    }

    With my original constructor of MyTreeWidget and your implementation of resizeEvent(), it works well, and the scroll bars do not flicker after the window is resized even if all items are expanded.
    So could you please let me see the code of your application containing the problem? Thank you very much.

    minorpan

  15. #14
    Join Date
    May 2006
    Location
    Finland
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get scrollbars in QTreeWidget

    OK minorpan! Here we go - the files mytreewidget.hpp and cpp are below.

    Launch the application, and then
    - click all tree nodes open
    - resize the window from the right-bottom corner so that the first leaf node
    (containing the text "aa" just above the long line) is on the right-bottom corner.
    The horizontal scroll bar "underlines" this line (so to speak).
    - then start resizing the window to the right. You know the long line of aaa's
    is just below the horizontal scrollbar.

    Do this a couple of times. Experiment with very small changes in the height of the
    window. I am sure you will get the flicker-effect and your CPU load rises up, right?

    - Jorma


    Qt Code:
    1. // the file mytreewidget.hpp:
    2. #include <QHeaderView>
    3. #include <QTreeWidget>
    4. #include <QApplication>
    5.  
    6. class MyTreeWidget: public QTreeWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MyTreeWidget(QWidget* parent = 0);
    12. QIcon nodeIcon;
    13. QIcon leafIcon;
    14.  
    15. virtual void resizeEvent( QResizeEvent* event);
    16.  
    17. protected slots:
    18. void updateColumnWidth();
    19.  
    20. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // the file mytreewidget.cpp:
    2. #include "mytreewidget.hpp"
    3.  
    4. MyTreeWidget::MyTreeWidget(QWidget* parent): QTreeWidget(parent)
    5. {
    6. // setSelectionMode( QAbstractItemView::SingleSelection);
    7. setDragEnabled(true); // inherited from QAbstractItemView
    8. setAcceptDrops(true);
    9. setDropIndicatorShown(true);
    10.  
    11.  
    12. setObjectName(QString::fromUtf8("objectView"));
    13. //setGeometry(QRect(10, 20, 120, 150));
    14. setColumnCount(2);
    15. header()->resizeSection(0, 0);
    16. setItemHidden(headerItem(), true);
    17.  
    18. QObject::connect(this, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(updateColumnWidth()));
    19. QObject::connect(this, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(updateColumnWidth()));
    20. QObject::connect(this, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(updateColumnWidth()));
    21. nodeIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
    22. QIcon::Normal, QIcon::Off);
    23. nodeIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon),
    24. QIcon::Normal, QIcon::On);
    25. leafIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
    26. }
    27.  
    28.  
    29. void MyTreeWidget::updateColumnWidth()
    30. {
    31.  
    32. header()->resizeSection(0, 0);
    33. resizeColumnToContents(0);
    34.  
    35. }
    36.  
    37. void MyTreeWidget::resizeEvent( QResizeEvent* event)
    38. {
    39. QTreeWidget::resizeEvent( event);
    40.  
    41. // in addition to plain old resizing update the column widths:
    42. updateColumnWidth(); // minorpan's function
    43. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4. MyTreeWidget widget;
    5.  
    6. pItem = new QTreeWidgetItem(&widget, QStringList("a"));
    7. Qt::ItemFlags flags = pItem->flags() & (~ Qt::ItemIsEditable)| Qt::ItemIsSelectable
    8. | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
    9. pItem->setFlags(flags);
    10. pItem->setIcon(0, widget.nodeIcon);
    11. pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
    12. pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
    13.  
    14. pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
    15. pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
    16.  
    17. pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
    18. pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
    19.  
    20. pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
    21. pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
    22.  
    23. pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
    24. pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
    25.  
    26. pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
    27. pItem->setFlags(flags);pItem->setIcon(0, widget.leafIcon);
    28.  
    29. pItem = new QTreeWidgetItem(&widget, QStringList("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
    30. pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
    31.  
    32. pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
    33. pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
    34.  
    35. pItem = new QTreeWidgetItem(pItem, QStringList("aa"));
    36. pItem->setFlags(flags);pItem->setIcon(0, widget.nodeIcon);
    37.  
    38.  
    39. widget.show();
    40. return app.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 5th May 2006 at 18:33. Reason: Added [code] tags

  16. The following user says thank you to jorma for this useful post:

    minorpan (5th May 2006)

  17. #15
    Join Date
    Apr 2006
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to get scrollbars in QTreeWidget

    Thank you.
    I have got the flicker-effect when running your demo application.
    I think it's important to resize the viewport when the contents displaying are resized, such as font sizes, right? Therefore the minorpan's function may still be called within these event handlers. I will try to make it better, thanks for your advice which is really excellent ^_^

    minorpan
    Last edited by minorpan; 5th May 2006 at 21:59.

Similar Threads

  1. how to sync a QTreeWidget and a QListWidget?
    By zl2k in forum Qt Programming
    Replies: 2
    Last Post: 5th September 2008, 20:55
  2. QTreeWidget Drag and Drop
    By tommydent in forum Qt Programming
    Replies: 10
    Last Post: 25th August 2008, 15:25
  3. QTreeWidget nextSibling()
    By user_mail07 in forum Newbie
    Replies: 1
    Last Post: 21st August 2008, 19:44
  4. QTreeWidget click
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 24th October 2007, 16:47
  5. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32

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.