Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
4.8.7, 5.6.3, 5.9.9, 5.12.7, 5.14.1
-
None
Description
#include <QApplication> #include <QListView> #include <QStringListModel> class ListView : public QListView { public: explicit ListView(QWidget *parent = nullptr) : QListView(parent) { setUniformItemSizes(true); } QSize sizeHint() const override { QMargins margins = contentsMargins(); return { QListView::sizeHint().width(), margins.top() + 10 * rectForIndex(indexAt(QPoint(0, 0))).height() + margins.bottom() }; } }; int main(int argc, char **argv) { QApplication app(argc, argv); QStringListModel model({"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}); ListView view; view.setModel(&model); view.show(); return QApplication::exec(); }
It is expected that the list view will have exactly the required height for 10 items (similar implementations of sizeHint() work for QTreeView and QTableView), but it shows up being a bit shorter.
QListView has a special handling of the frame width in qwindowsxpstyle.cpp so it calls QFramePrivate::updateStyledFrameWidths in its constructor. This function updates *FrameWidth values but doesn't update contentsMargins unlike QFramePrivate::updateFrameWidth used by QFrame itself.
The latter function has suggested a workaround — call setFrameRect({}) after the QListView constructor.