Results 1 to 5 of 5

Thread: resize to content of a QTreeWidget

  1. #1
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    4
    Qt products
    Qt4

    Default resize to content of a QTreeWidget

    I have a widget with a QLineEdit and a QTreeWidget in a QVBoxLayout. I would like to size MyWidget to adjust to the longest string in the treeWidget.
    Do I need to override sizeHint() or can qt do it somehow? When I type in the QLineEdit, it automatically adjusts the items in QTreeWidget.

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    2. {
    3. setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
    4.  
    5. QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
    6. vBoxLayout->setContentsMargins(1, 1, 0, 0);
    7. mLineEdit = new QLineEdit;
    8.  
    9. vBoxLayout->addWidget(mLineEdit);
    10.  
    11. mTreeWidget = new MyTreeWidget;
    12. mTreeWidget->setWindowFlags(Qt::Popup);
    13. mTreeWidget->setFocusPolicy(Qt::NoFocus);
    14. mTreeWidget->setMouseTracking(true);
    15. mTreeWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    16.  
    17. mTreeWidget->setColumnCount(1);
    18. mTreeWidget->setUniformRowHeights(true);
    19. mTreeWidget->setRootIsDecorated(false);
    20. mTreeWidget->setEditTriggers(QTreeWidget::NoEditTriggers);
    21. mTreeWidget->setSelectionBehavior(QTreeWidget::SelectRows);
    22. mTreeWidget->setFrameStyle(QFrame::Box | QFrame::Plain);
    23. mTreeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    24. mTreeWidget->header()->hide();
    25. mTreeWidget->resizeColumnToContents(0);
    26.  
    27. mLineEdit->installEventFilter(this);
    28. vBoxLayout->addWidget(mTreeWidget);
    29. connect(mLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateList(const QString&)));
    30. connect(mTreeWidget, SIGNAL(itemClicked( QTreeWidgetItem*, int)), SLOT(updateLineEdit(QTreeWidgetItem*, int)));
    31. move(QCursor::pos());
    32. show();
    33. mLineEdit->setFocus();
    34. setLayout(vBoxLayout);
    35. layout()->setSizeConstraint(QLayout::SetFixedSize);
    36. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    4
    Qt products
    Qt4

    Default Re: resize to content of a QTreeWidget

    Maybe I wasn't clear: the second item has the longest string and I would expect the QTreeWidget to grow to show the whole string.
    I added an example with the content of the QTreeWidget.
    Any ideas why it doesn't resize to the longest string?


    Qt Code:
    1. #include <QVBoxLayout>
    2. #include <QLineEdit>
    3. #include <QTreeWidget>
    4. #include <QLineEdit>
    5. #include <QTreeWidget>
    6.  
    7. MyWidget::MyWidget(QWidget *parent)
    8. : QWidget(parent)
    9. {
    10. setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
    11.  
    12. QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
    13. vBoxLayout->setContentsMargins(1, 1, 0, 0);
    14. QLineEdit *mLineEdit = new QLineEdit;
    15.  
    16. vBoxLayout->addWidget(mLineEdit);
    17.  
    18. QTreeWidget *treeWidget = new QTreeWidget;
    19. item->setText(0, "The quick brown fox jumps over the lazy dog");
    20. treeWidget->addTopLevelItem(item);
    21. item2->setText(0, "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog");
    22. treeWidget->addTopLevelItem(item2);
    23. item3->setText(0, "The quick brown");
    24. treeWidget->addTopLevelItem(item3);
    25.  
    26.  
    27. treeWidget->setWindowFlags(Qt::Popup);
    28. treeWidget->setFocusPolicy(Qt::NoFocus);
    29. treeWidget->setMouseTracking(true);
    30. treeWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    31.  
    32. treeWidget->setColumnCount(1);
    33. treeWidget->setUniformRowHeights(true);
    34. treeWidget->setRootIsDecorated(false);
    35. treeWidget->setEditTriggers(QTreeWidget::NoEditTriggers);
    36. treeWidget->setSelectionBehavior(QTreeWidget::SelectRows);
    37. treeWidget->setFrameStyle(QFrame::Box | QFrame::Plain);
    38. treeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    39. treeWidget->header()->hide();
    40. treeWidget->resizeColumnToContents(0);
    41.  
    42. mLineEdit->installEventFilter(this);
    43. vBoxLayout->addWidget(treeWidget);
    44. connect(mLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateList(const QString&)));
    45. connect(treeWidget, SIGNAL(itemClicked( QTreeWidgetItem*, int)), SLOT(updateLineEdit(QTreeWidgetItem*, int)));
    46. move(QCursor::pos());
    47. show();
    48. mLineEdit->setFocus();
    49. setLayout(vBoxLayout);
    50. layout()->setSizeConstraint(QLayout::SetFixedSize);
    51. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    4
    Qt products
    Qt4

    Default Re: resize to content of a QTreeWidget

    Does nobody have an idea? Please let me know if I'm not clear or if you need more code sample.

    Thanks!

  4. #4
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    4
    Qt products
    Qt4

    Default Re: resize to content of a QTreeWidget

    Any ideas?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,688
    Thanks
    13
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: resize to content of a QTreeWidget

    There is no natural size for a tree view. A QTreeView is a QAbstractScrollArea. If the content, i.e. the table cells, grow larger than the area available to display it, dictated by the containing layout, then the scroll area gains scroll bars. If you force the horizontal scroll bars to stay hidden then you will have no way to access data outside the current view port. Under no circumstance will the table view automatically resize itself; its sizeHint() is not derived from its content.

    If you want a column to automatically occupy all the remaining space within the current view port then you want to set the section's resize mode QHeaderView::setResizeMode() to QHeaderView::Stretch. If you want the column to always match the content within it then set the resize mode to QHeaderView::ResizeToContents. The QTreeView::setWordWrap() function may also be of use to make longer items viewable. None of these options changes the size of the QTreeView.

    If you really want to resize tree view based on its content then you will need to code to calculate the "correct" size and force it. Set all the sections to QHeaderView::ResizeToContents and query them for their size after every data update to get a reasonable starting point. No matter how much sense it makes you eventually hit the point where the content is too wide for the screen and you are back to where you started.
    Last edited by ChrisW67; 23rd April 2013 at 05:42.

  6. The following user says thank you to ChrisW67 for this useful post:

    torres (29th April 2013)

Similar Threads

  1. How can QTreeWidget force window to resize.
    By Malutek in forum Newbie
    Replies: 2
    Last Post: 4th May 2012, 09:36
  2. Replies: 6
    Last Post: 20th May 2011, 18:56
  3. QTreeWidget not updating content...
    By lni in forum Qt Programming
    Replies: 7
    Last Post: 14th August 2010, 17:06
  4. Resize Row Hieght in QTreeWidget
    By photo_tom in forum Qt Programming
    Replies: 0
    Last Post: 24th May 2010, 16:42
  5. Resize QTreeWidget columns to contents
    By FaS in forum Qt Programming
    Replies: 9
    Last Post: 2nd July 2006, 21:48

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.