currentItem方法返回列表部件當前選擇的項,setCurrentItem方法用於設置當前項。語法如下:
- QListWidgetItem currentItem()
- setCurrentItem(QListWidgetItem item)
- setCurrentItem(QListWidgetItem item, QItemSelectionModel.SelectionFlags command)
關於SelectionFlags 請參考《PyQt(Python+Qt)學習隨筆:Mode/View中的枚舉類 QItemSelectionModel.SelectionFlag取值及含義》,帶SelectionFlags 參數時除了設置當前項外,還會影響對列表部件的項的選中數據變化,具體變化由參數command指定。
setCurrentItem使用時注意2點:
- 除非選擇模式設置為NoSelection,否則setCurrentItem設置項當前項后,對應項也會被選中
- setCurrentItem沒有返回值,無法判斷是否成功識別,如果對應item在列表部件中不存在也不會有異常出現