在 Python/PySide 中清除选定的列表项(因此没有选定的列表项)

发布于 2024-12-19 10:08:53 字数 702 浏览 3 评论 0原文

我有一个使用 PySide 用 Python 编写的应用程序,UI 是使用 QT Designer 创建的。

我在窗口中有一个列表,其中包含一组项目。当应用程序加载时,它似乎抓取了列表中的最后一项(即使列表中没有显示“突出显示”行)。如果我中断并检查当时是否有选定的项目,我将获得列表中的最后一个项目。

我有这样的代码:

    @QtCore.Slot(QTreeWidgetItem,QTreeWidgetItem)
    def on_list_currentItemChanged(self,current=None,previous=None):
        if current:
            self.ui.actionDelete_Task.setEnabled(True)
        else:
            self.ui.actionDelete_Task.setEnabled(False)

它确定是否选择了当前项目。因此,在初始加载时,我希望 current 为空,但事实并非如此,它指向列表中的最后一项,即使没有选择任何项目。

我缺少什么吗?或者,我如何告诉列表没有选定的行/取消选择所有行?

最终我要寻找的是(a)当表单加载并且列表最初显示没有选定的项目时,删除按钮被禁用,甚至(b)如果有人应该通过此操作删除项目,则会赢得之后就不再是选定的项目(因此他们不能只按两次删除键并按顺序删除两行)。

I have an application written in Python using PySide, the UI was created with QT Designer.

I have a list in the window, which has a set of items. When the application loads, it seems to grab the last item in the list (even though there is no 'highlighted' row showing in the list). If I break and check if there is a selected item at that time, I will get the last item in the list.

I have this code:

    @QtCore.Slot(QTreeWidgetItem,QTreeWidgetItem)
    def on_list_currentItemChanged(self,current=None,previous=None):
        if current:
            self.ui.actionDelete_Task.setEnabled(True)
        else:
            self.ui.actionDelete_Task.setEnabled(False)

Which determines if there is a current item selected. So upon initial load, I expect current to be empty, but it's not, it's pointing to the last item in the list, even though no item has been selected.

Is there something that I'm missing? Or, alternatively, how do I tell the list to not have a selected row/deselect all rows?

Ultimately what I'm looking for is (a) that when the form loads and the list is originally shown with no selected item, that the delete button is disabled and even (b) if someone should delete an item through this action, there won't be a selected item after that (so they couldn't just hit delete twice and delete two rows in sequence).

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

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

发布评论

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

评论(1

樱花落人离去 2024-12-26 10:08:53

当前项目和选择不一定是同一件事。当前项是指键盘焦点所在的项;该选择是指当前突出显示的项目。因此,没有选择的树小部件仍将具有当前项目(除非它根本没有项目)。

因此,一种解决方案可能是简单地检查当前项目是否也被选中:

@QtCore.Slot(QTreeWidgetItem, QTreeWidgetItem)
def on_list_currentItemChanged(self, current=None, previous=None):
    if current is not None and current.isSelected():
        self.ui.actionDelete_Task.setEnabled(True)
    else:
        self.ui.actionDelete_Task.setEnabled(False)

EDIT

查看 Qt 源代码后,上面的代码似乎不起作用,因为 currentItemChanged< /code> 信号在选择重置之前发出。

因此,我建议使用 itemSelectionChanged 信号,并检查 selectedItems

def on_list_ItemSelectionChanged(self):
    if self.list.selectedItems():
        self.ui.actionDelete_Task.setEnabled(True)
    else:
        self.ui.actionDelete_Task.setEnabled(False)

注意:采用这种方法可能还需要替换 currentItem()< /code> 与代码中其他地方的 selectedItems()[0]

The current item and the selection are not necessarily the same thing. The current item refers to the item with the keyboard focus; the selection refers to the currently highlighted items. So a tree widget with no selection will still have a current item (unless it has no items at all).

So one solution might be to simply check that the current item is also selected:

@QtCore.Slot(QTreeWidgetItem, QTreeWidgetItem)
def on_list_currentItemChanged(self, current=None, previous=None):
    if current is not None and current.isSelected():
        self.ui.actionDelete_Task.setEnabled(True)
    else:
        self.ui.actionDelete_Task.setEnabled(False)

EDIT

After taking a look at the Qt source code, it appears the above code won't work because the currentItemChanged signal is emitted before the selection is reset.

So, instead, I would suggest using the itemSelectionChanged signal, and checking the selectedItems:

def on_list_ItemSelectionChanged(self):
    if self.list.selectedItems():
        self.ui.actionDelete_Task.setEnabled(True)
    else:
        self.ui.actionDelete_Task.setEnabled(False)

NB: taking this approach may also entail replacing the usage of currentItem() with selectedItems()[0] elsewhere in your code.

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