QListWidget that resizes instead of scrolls

How do you change the behavior of a QListWidget so that it changes its height instead of selecting a (apparently arbitrary) height and adding scrollbars? See screenshot:

screen shot

QListView should fill as much horizontal space as possible (creating as many “columns” if you do.) Then they wrap and make as many rows as necessary to fit all the elements. These calculations should be adjusted as the window resizes. It all works fine.

However, what I want is that instead of keeping the height the same, the QListView should grow or shrink vertically and never need to scroll. Scrolling, if necessary, will be handled in the parent QWidget , which contains all the labels and lists. It seems that as soon as the height of the QListWidget (not sure where its default value QListWidget from), it never changes. In some cases, it is too large (see the second list of "Test" above) and too small in others (see the first list of "empty cards" above).

The markup above is not surprising: two QLabel and two QListWidget in a QVBoxLayout. Here are the properties that I set on QListWidget :

 setMovement(QListView::Static); setResizeMode(QListView::Adjust); setViewMode(QListView::IconMode); setIconSize(QSize(128, 128)); 

(I already tried setting horizontal and vertical scroll policies, but that just turns off the scroll bars, cutting off the content. Not what I want.)

+6
c ++ qt qlistview qlistwidget
source share
2 answers

Perhaps you could have done this without using QListWidget. The Qt examples contain a new QFlowLayout layout class that may be useful. Using the following hierarchy of widgets, you can get several groups with labels, and all of them will be inside one QScrollArea.

 QScrollBox QVBoxLayout QLabel "Blank maps" QWidget QFlowLayout your own widgets showing map images and labels QLabel "Text" QWidget QFlowLayout your own widgets 

The problem is that such a solution will create much more widgets than a solution based on QListWidget. Therefore, if you have hundreds of items on your list, this may not be the best solution.

+3
source share

QListView has a protected member function called contentsSize() . It is used to calculate the required minimum() , maximum() and pageStep() for scrollbars (as mentioned here ).

Can you subclass the QListView class and use this information? I suggest you recount the size of your widget in the same function where you add content to it. While a little lacking in elegance, this seems to be a pretty reliable solution.

+3
source share

All Articles