Results 1 to 15 of 15

Thread: How to get scrollbars in QTreeWidget

Hybrid View

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

    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()));

  2. #2
    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

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

    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

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

    minorpan (5th May 2006)

  5. #4
    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
  •  
Qt is a trademark of The Qt Company.