如何更改 QListWidget 的行为,以便调整其高度大小,而不是选择(看似任意)高度并添加滚动条?请参阅屏幕截图:
QListView
应尽可能填充水平空间(如果愿意的话,可以创建尽可能多的“列”。)然后,它们会根据需要换行并制作尽可能多的行以适合所有项目。这些计算应随着窗口大小的调整而调整。这一切工作正常。
然而,我想要发生的是,QListView
应该垂直增长或收缩,而不是高度保持不变,并且永远不需要任何滚动条。如有必要,滚动将在托管所有标签和列表的父 QWidget
上处理。似乎一旦建立了 QListWidget 的高度(不确定其默认值来自哪里),它就永远不会改变。在某些情况下它太大(请参见上面的第二个“测试”列表),而在其他情况下则太小(请参见上面的第一个“空白地图”列表。)
上面的布局并不奇怪:两个 QLabel
和 QVBoxLayout 中的两个 QListWidget
。
以下是我在 QListWidget
上设置的属性:(
setMovement(QListView::Static);
setResizeMode(QListView::Adjust);
setViewMode(QListView::IconMode);
setIconSize(QSize(128, 128));
我已经尝试设置水平方向和垂直滚动条策略,但这只是关闭滚动条,剪辑内容不是我想要的。)
How do you change the behavior of a QListWidget
so that it resizes its height instead of choosing a (seemingly arbitrary) height and adding scrollbars? See screenshot:
The QListView
's should fill up as much space horizontally as they can (creating as many "columns," if you will.) Then they wrap and make as many rows as necessary to fit all the items. These calculations should be adjusted as the window is resized. This is all working fine.
However, what I want to happen is that instead of the height staying the same, the QListView
should grow or shrink vertically and never need any scrollbars. The scrolling, if necessary, will be handled on the parent QWidget
that hosts all of the labels and lists. It seems like once the height of the QListWidget
is established (not sure where its default is coming from), it never changes. It is too big in some cases (see second "Test" list above) and too small in others (see first "blank maps" list above.)
The layout above is nothing surprising: two QLabel
's and two QListWidget
's in a QVBoxLayout.
Here are the properties I have set on the QListWidget
's:
setMovement(QListView::Static);
setResizeMode(QListView::Adjust);
setViewMode(QListView::IconMode);
setIconSize(QSize(128, 128));
(I already tried setting the horizontal and vertical scrollbar policies, but that just turns the scrollbars off, clipping the content. Not what I want.)
发布评论
评论(2)
也许你可以在不使用 QListWidget 的情况下做到这一点。 Qt 的示例包含一个新的布局类 QFlowLayout,它可能很有用。使用以下一种小部件层次结构,您可以获得多个带有标签的组,并且它们都将位于一个 QScrollArea 内。
问题是这种解决方案会比基于 QListWidget 的解决方案创建更多的小部件。因此,如果您的列表中有数百个项目,这可能不是最佳解决方案。
Maybe you could this without using QListWidget. The Qt's examples contain a new layout class, QFlowLayout, which could be useful. With the following kind of widget hierarchy you could get multiple groups with labels and they all would be inside one QScrollArea.
The problem is that this kind of solution would create much more widgets than QListWidget based solution. So if you have hundreds of items in your list, this might not be the best solution.
QListView
中有一个名为contentsSize()
的受保护成员函数。它用于计算滚动条所需的minimum()
、maximum()
和pageStep()
(如前所述此处)。您可以子类化
QListView
类并利用该信息吗?我建议您在向其添加内容的同一函数中重新计算小部件的大小。虽然有些缺乏优雅,但这似乎是一个相当可靠的解决方案。There is a protected member function called
contentsSize()
inQListView
. It is used to calculate the requiredminimum()
,maximum()
, andpageStep()
for the scrollbars (as mentioned here).Can you subclass the
QListView
class and make use of that information? I suggest you recalculate the size of your widget in the same function where you add contents to it. While somewhat lacking elegance, this appears to be a pretty reliable solution.