Results 1 to 7 of 7

Thread: resizing a QTreeWidget

  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Unhappy resizing a QTreeWidget

    I'm trying to get a QTreeWidget to show a text column and some equally sized checkbox columns. This eventually works, but the QTreeWidget seems hell-bent on showing up at 256x192 pixels, although the stuff put in it requires less space.

    QWidget::adjustSize() claims to resize a widget to fit the contents, but does not.
    Calling resize() on the QTreeWidget helps if it is a top level widget, but does nothing if it is in a layout together with other widgets.

    It is possible to let the user manually adjust the size after it is rendered, but how does one resize a QTreeWidget to fit its contents from code? (Tested in Qt-4.2.0-rc1)

    /Joakim Rosqvist


    Qt Code:
    1. #include <QApplication>
    2. #include <QTreeWidget>
    3. #include <QHeaderView>
    4. #include <QHBoxLayout>
    5.  
    6. int main( int argc, char **argv )
    7. {
    8. QApplication app( argc, argv );
    9.  
    10. QWidget *hbox = new QWidget;
    11. hbox->setLayout(lay = new QHBoxLayout);
    12.  
    13. tw->setColumnCount(3);
    14. twi->setText(0,"Text");
    15. twi->setCheckState(1,Qt::Checked);
    16. twi->setCheckState(2,Qt::Checked);
    17. tw->addTopLevelItem(twi);
    18. tw->resizeColumnToContents(0);
    19. tw->resizeColumnToContents(1);
    20. tw->resizeColumnToContents(2);
    21. tw->header()->setResizeMode(0, QHeaderView::Interactive);
    22. tw->header()->setResizeMode(1, QHeaderView::ResizeToContents);
    23. tw->header()->setResizeMode(2, QHeaderView::ResizeToContents);
    24. tw->header()->setStretchLastSection(false);
    25.  
    26. tw->adjustSize(); // no effect!
    27. tw->resize(110,100); // no effect!
    28.  
    29. lay->addWidget(tw);
    30. hbox->show();
    31. app.exec();
    32. return 0;
    33. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: resizing a QTreeWidget

    You have to set sizeHint and/or sizePolicy. It is possible that setMinimumSize() along with size policy set to "Minimum" is enough to achieve what you need.

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

    drhex (1st October 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: resizing a QTreeWidget

    I tried playing around with sizePolicy and minimumSize, but that did not accomplish anything useful.

    Reimplementing sizeHint works better, e.g.:

    Qt Code:
    1. QSize MyTreeWidget::sizeHint() const
    2. {
    3. int width=0;
    4. for (int i=0; i<columnCount(); ++i)
    5. width += 2 + columnWidth(i);
    6. return QSize(width +16-2, 100);
    7. }
    To copy to clipboard, switch view to plain text mode 

    That is, I basically add up the column-widths. Unfortunately I had to hardcode in "2" for the spacing between columns that is not included in the columnWidth(), and a "16" for the width of a scrollbar. Is there any way to ask Qt about these numbers?

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

    Default Re: resizing a QTreeWidget

    Quote Originally Posted by drhex View Post
    Is there any way to ask Qt about these numbers?
    "2" for the spacing between columns
    Probably not.

    "16" for the width of a scrollbar
    verticalScrollbar()->width() should do the trick. Just check if the scrollbar should be visible at all.

  6. #5
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: resizing a QTreeWidget

    Just check if the scrollbar should be visible at all.
    ...by knowing how many item one wants to show and how much space each
    of them will use up and how much space there is available, I presume.

    Qt doesn't seem to know [whether a scrollbar is needed] by the time sizeHint is called;
    The scrollbar width() always returns 100 there, it changes into 16 once it is actually rendered on screen.

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

    Default Re: resizing a QTreeWidget

    Quote Originally Posted by drhex View Post
    ...by knowing how many item one wants to show and how much space each
    of them will use up and how much space there is available, I presume.
    I don't know is such assumptions are appropriate. Calling verticalScrollBar()->isVisible() might be more reliable.

    Qt doesn't seem to know [whether a scrollbar is needed] by the time sizeHint is called;
    That doesn't matter. You can change the size hint once the widget is rendered for the first time using QWidget::updateGeometry().
    The scrollbar width() always returns 100 there, it changes into 16 once it is actually rendered on screen.
    Yes, that's normal for every widget.

  8. The following user says thank you to wysota for this useful post:

    drhex (1st October 2006)

  9. #7
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: resizing a QTreeWidget

    Just found a way to get the size of the scrollbar without having to display one first

    Qt Code:
    1. qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent)
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 21st September 2006, 10:37
  2. QTreeWidget & QListWidget different selection
    By munna in forum Qt Programming
    Replies: 9
    Last Post: 21st July 2006, 06:50
  3. How to capture resizing of QTreeWidget columns?
    By simk in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2006, 06:10
  4. QTreeWidget problem!!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2006, 11:47
  5. few questions related to QTreeWidget
    By prakash in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2006, 07: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.