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】