November 23, 2010

calberto calberto
Lab Rat
18 posts

QTreeWidget: how to limit the number of displayed rows?

 

With the Designer, I have placed two QTreeWidget on a QVBoxLayout. I would like to force the upper one to display only 4 rows, giving more space to the lower one. I can obtain something of acceptable by limiting the maximum size with the designer, but I would like to be independent on the pixel and limits in term of rows, in particular to the number of used rows.

How can I do?

6 replies

November 23, 2010

Volker Volker
Robot Herder
5428 posts

As far as I know there is no easy way to achieve this. One has to account for the frame size of the scroll area, individual row heights (including font size, multi-line rows), header height, scroll bars and probably more in a platform independent way.

November 23, 2010

Antonio Di Monaco Antonio Di Monaco
Lab Rat
116 posts

Hi,

you should use QTreeView, and implement your custom model. So you can choose the number of “rowCount” for each parent node in the tree.

T.

November 24, 2010

dfaure dfaure
Lab Rat
41 posts

I suppose you could also insert a QSortFilterProxyModel-derived class and reimplement filterAcceptRows in order to reject all rows which have index.row() > 4.

 Signature 

David Faure (.(JavaScript must be enabled to view this email address))
KDE/Qt Senior Software Engineer
KDAB - Qt Experts - Platform-independent software solutions

November 24, 2010

iunknwn iunknwn
Lab Rat
34 posts

in designer on a upper QTreeWidget, Make minimumSize and maximumSize Height a same number as geometry height.

Add four rows and do a quick preview (ctrl + r). Come back and re-adjust that number based on how it previews. Once you are satisfied, you’ve your perfect locked-down state. If you resize the widget, this treewidget wouldn’t resize.

Alternately, you could set the vertical sizepolicy as fixed.

 Signature 

Vista x64 with Qt 4.8.2 and VS2010

November 25, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

iunknwn: it is not a good solution. Rows can have different heights, in other OSes there can be another sizes in styles and so on.

November 25, 2010

Volker Volker
Robot Herder
5428 posts

The proxy filter model solutions seems not to be what the user wants. The tree widget should have a height to show 4 rows simultaneously and provide a scroll bar if there are more than 4 rows to display.

Another solution might be to use a QSplitter [doc.qt.nokia.com], let the user choose the ratio between the two widgets and save the splitter’s state when closing the window or quitting the application.

See
QSplitter::saveState() [doc.qt.nokia.com]
QSplitter::restoreState() [doc.qt.nokia.com]
QSettings [doc.qt.nokia.com]
for details.

 
  ‹‹ Rearranging/shifting widgets in grid layout      Win-1251 codepage and QIBASE driver ››

You must log in to post a reply. Not a member yet? Register here!