How to automatically adjust the size of QListView?
-
I'm coding a Qt Widgets desktop app and I have a simple QListView with a custom model and a delegate. I want the QListWidget size to resize automatically when new elements are added to it, such that in the following image, the + button appears just below the last QLineEdit delegate item.
There is a vertical layout that owns the list and a button.
I tried reimplementing the
sizeHint
function according to this topic: (tried different answers)
https://stackoverflow.com/questions/25613456/adjust-the-height-of-qlistview-to-fit-the-contentI also called
updateGeometry()
on the view, but nothing of the above worked for me. Even If I return a constant value insizeHint
, the size doesn't change.Any direction, please?
P.S I tried to look for similar questions on Qt forum, but found no solution so far
-
Hi and welcome to devnet,
Do you mean you want your QListView to expand so that all items are always visible ?
-
Do you mean you want your QListView to expand so that all items are always visible ?
I want my QListView to always fit size of it's actual contents, for example if the list has two rows, the height should be exactly sufficient to contain these two rows and no more. If I add elements to the model or remove them, I expect the widget to be resized respectively to the number of rows.
(I want the button to be just below the last row and move with new rows added to the list), and I wonder if I even need to play with the list size.
Thank you in advance!
-
Thank you, the issue was solved by changing the stretch factors.
It was enough to change
listLayout->addWidget(button);
to
listLayout->addWidget(button,1,Qt::AlignTop);
So the stretch factor ratio 1:0 allows to allign the button on the top of it's area. -