1. QListWidget 有时出现最后多一个空白行问题
举例: 假设 QListWidget 的高度为150, border为1, item的高度为30,共有10个item。当将竖直滚动条滑到底部,会发现有一个空白行。
方案: 需要QListWidget一页(或者其高度范围)必须正好能显示整数个item即可。初看上去例子中一页正好显示5个item,其实不是,因为上下 border 各需1px。把 QListWidget 的高度调整为152,问题解决。
2. QListWidgetItem
(1)QListWidgetItem 有一个可设置 icon 和 text 的构造函数。
(2)QListWidgetItem 可通过 setSizeHint 设置尺寸。
(3)QListWidgetItem 可通过 setData 设置一些用户数据,并通过 Data 取出。 注意参数1从 Qt::UserRole 开始。
Constant | Value | Description |
Qt::UserRole | 0x0100 | The first role that can be used for application-specific purposes. |
For user roles, it is up to the developer to decide which types to use and ensure that components use the correct types when accessing and setting data.
(4)图标大小通过 QListWidget 的 setIconSize 设置。
iconSize : QSize
This property holds the size of items' icons
Setting this property when the view is visible will cause the items to be laid out again.
(5)可通过 QListWidget 的 setItemWidget 在某个 item 中展示 widget。
【参考Qt5.9.2 Assistant】