QListWidget 调整大小而不是滚动

发布于 2024-09-18 05:35:52 字数 835 浏览 10 评论 0 原文

如何更改 QListWidget 的行为,以便调整其高度大小,而不是选择(看似任意)高度并添加滚动条?请参阅屏幕截图:

screen shot

QListView 应尽可能填充水平空间(如果愿意的话,可以创建尽可能多的“列”。)然后,它们会根据需要换行并制作尽可能多的行以适合所有项目。这些计算应随着窗口大小的调整而调整。这一切工作正常。

然而,我想要发生的是,QListView应该垂直增长或收缩,而不是高度保持不变,并且永远不需要任何滚动条。如有必要,滚动将在托管所有标签和列表的父 QWidget 上处理。似乎一旦建立了 QListWidget 的高度(不确定其默认值来自哪里),它就永远不会改变。在某些情况下它太大(请参见上面的第二个“测试”列表),而在其他情况下则太小(请参见上面的第一个“空白地图”列表。)

上面的布局并不奇怪:两个 QLabelQVBoxLayout 中的两个 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:

screen shot

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

闻呓 2024-09-25 05:35:52

也许你可以在不使用 QListWidget 的情况下做到这一点。 Qt 的示例包含一个新的布局类 QFlowLayout,它可能很有用。使用以下一种小部件层次结构,您可以获得多个带有标签的组,并且它们都将位于一个 QScrollArea 内。

QScrollBox
  QVBoxLayout

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

    QLabel "Text"
    QWidget
       QFlowLayout
          your own widgets

问题是这种解决方案会比基于 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.

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 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.

很快妥协 2024-09-25 05:35:52

QListView 中有一个名为 contentsSize() 的受保护成员函数。它用于计算滚动条所需的 minimum()maximum()pageStep()(如前所述此处)。

您可以子类化QListView 类并利用该信息吗?我建议您在向其添加内容的同一函数中重新计算小部件的大小。虽然有些缺乏优雅,但这似乎是一个相当可靠的解决方案。

There is a protected member function called contentsSize() in QListView. It is used to calculate the required minimum(), maximum(), and pageStep() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文