通常,您将连接到itemSelectionChanged
信号,然后检查是否选择了任何内容。
self.listwidget.itemSelectionChanged.connect(self.on_selection_changed)
def on_selection_changed(self):
if not self.listwidget.selectedItems():
# Do Stuff Here
self.nothing_selected_function()
但是,这只会赶上那里的东西被选择,然后取消选择用户的一切活动。如果没有选择任何东西,它不会触发这个信号(就像第一次建立列表一样,没有选择任何东西)。在这种情况下,你必须手动调用插槽。
self.listwidget = ... # Code that builds and populates list widget
# Call this manually the first time.
self.on_selection_changed()
但是你的问题的一部分是不明确的。 为什么你想知道什么时候“未选中”?什么时候将新项目添加到列表中?它应该触发你的“未选定”功能,因为列表已经改变,但是还没有选择任何东西?
每当没有选择的项目,我想禁用一些按钮和文本框。当用户将新项目添加到列表中时,它会自动将该项目设置为选定项目。 – Gambit2007