简体   繁体   English

如何从QListWidget获取当前选定的项目,而不是它记得的内容

[英]How to get currently selected item from QListWidget and not what it remembers

QListWidget returns on .selectedItems() even if there are no items currently selected (it remembers last item clicked or selected. So even if all its items were deselected it still returns what it remembers). QListWidget返回.selectedItems()即使当前没有选择任何项(它会记住上一次单击或选中的项。因此,即使取消选择了所有项,它仍会返回其记住的内容)。 But I need QListWidget to return ONLY that item that is currently selected. 但是我需要QListWidget返回仅当前选中的那个项目。

.hasFocus() trick is not reliable since all the items could get hidden, QListWidget would be in focus. .hasFocus()技巧不可靠,因为所有项目都可能被隐藏,QListWidget将成为焦点。 But it still will go ahead and return an item while nothing is selected. 但是在什么都没有选择的情况下,它仍然可以继续并返回一个项目。

I'm not sure why you don't think that .selectedItems() doesn't work. 我不确定为什么您不认为.selectedItems()不起作用。 I just tried this with the code below and it's working correctly. 我只是用下面的代码尝试了一下,它可以正常工作。

import sys

from PySide import QtGui


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super().__init__()

        self.resize(720, 480)
        central_widget = QtGui.QWidget(self)
        self.setCentralWidget(central_widget)

        layout = QtGui.QHBoxLayout(central_widget)

        self.text_edit = QtGui.QTextEdit(central_widget)
        layout.addWidget(self.text_edit)

        self.drop_list = QtGui.QListWidget(central_widget)
        self.drop_list.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
        self.drop_list.addItems(['one', 'two', 'three', 'four'])
        self.drop_list.itemSelectionChanged.connect(self.show_List)
        layout.addWidget(self.drop_list)

        statusbar = QtGui.QStatusBar(self)
        self.setStatusBar(statusbar)

        action_ShowList = QtGui.QAction(self)
        action_ShowList.triggered.connect(self.show_List)

        self.show()

    def show_List(self):
        self.text_edit.setText(repr(self.drop_list.selectedItems()))


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    frame = MainWindow()
    sys.exit(app.exec_())

Everything selected: 选择的所有内容:

列表中的所有项目均已选中

Nothing selected: 没有选择:

列表中未选择任何项目

I ended up using this approach: 我最终使用了这种方法:

def getSelectedItem(self):
    if not self.myTreeWidget.hasFocus(): return
    for selectedItem in self.myTreeWidget.selectedItems():
        if not selectedItem: continue
        if selectedItem.isHidden(): continue 
        return selectedItem

Edited later: 以后编辑:

Here is his code (edited) showing the problem I've mentioned. 这是他的代码(已编辑),显示了我提到的问题。

First select an item, then hide all the items by hitting 'Hide-Unhide' button. 首先选择一个项目,然后单击“隐藏-取消隐藏”按钮隐藏所有项目。 Click anywhere inside of now being empty listView (just to make sure everything is deselected). 单击现在为空的listView内部的任何位置(只是确保取消选中所有内容)。 Click "Print Selected" button. 单击“打印所选”按钮。 Here is the image of the result: 这是结果的图像:

在此处输入图片说明

from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.resize(720, 480)
        central_widget = QtGui.QWidget(self)
        self.setCentralWidget(central_widget)

        layout = QtGui.QHBoxLayout(central_widget)

        self.text_edit = QtGui.QTextEdit(central_widget)
        layout.addWidget(self.text_edit)

        self.drop_list = QtGui.QListWidget(central_widget)

        self.drop_list.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
        self.drop_list.addItems(['one', 'two', 'three', 'four'])
        layout.addWidget(self.drop_list)
        self.show()

        self.button1=QtGui.QPushButton("Hide-Unhide Items")
        self.button1.clicked.connect(self.hideUnhideItems)
        layout.addWidget(self.button1)

        self.button2=QtGui.QPushButton("Print Selected")
        self.button2.clicked.connect(self.getSelected)
        layout.addWidget(self.button2)

    def getSelected(self):
        self.text_edit.clear()
        self.text_edit.setText(repr(self.drop_list.selectedItems()))

    def hideUnhideItems(self):
        for i in range(self.drop_list.count()):
            item=self.drop_list.item(i)
            if not item.isHidden():
                item.setHidden(True)
            else:
                item.setHidden(False)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    frame = MainWindow()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM