如何使用pyqt4在列表视图中获取选择项

时间:2019-03-19 07:00:53

标签: python-2.7 pyqt4

这是我的示例代码。当我在列表视图中单击索引项时,我得到了选择项,它工作正常。但是我想使用向上和向下箭头来获取所选项。谁能帮帮我吗。预先谢谢你。

下面是我的代码:

import sys
from PyQt4 import QtCore,QtGui
class mtable(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.auto_search1 = QtGui.QWidget()
        self.auto_search_vbox1 = QtGui.QVBoxLayout(self.auto_search1)
        self.auto_search_vbox1.setAlignment(QtCore.Qt.AlignLeft)
        hbox1=QtGui.QHBoxLayout()
        self.le_search1 = QtGui.QLineEdit()
        self.se_btn1 = QtGui.QPushButton("Search")
        self.searchBtn = QtGui.QPushButton("Close")
        self.searchBtn.clicked.connect(self.auto_search1.close)
        self.se_btn1.clicked.connect(self.filterClicked1)
        hbox1.addWidget(self.le_search1)
        hbox1.addWidget(self.se_btn1)
        hbox1.addWidget(self.searchBtn)
        self.auto_search_vbox1.addLayout(hbox1)
        self.total_list1 =[]
        self.list1 = QtGui.QListView()
        self.list1.clicked.connect(self.on_treeView_clicked)
        self.model1 = QtGui.QStandardItemModel(self.list1)
        self.y =['one','two', 'three']
        for i in self.y:
            self.total_list1.append(i)
        for code in  self.total_list1:
            item1 = QtGui.QStandardItem(code)
            self.model1.appendRow(item1)
        self.list1.setModel(self.model1)
        self.auto_search_vbox1.addWidget(self.list1)
        self.auto_search1.show()
        self.auto_search1.resize(1000,500)
    @QtCore.pyqtSlot(QtCore.QModelIndex)
    def on_treeView_clicked(self, index):
        itms = self.list1.selectedIndexes()
        for data in itms:
            print index.data().toString()
            self.le_search1.setText(index.data().toString())
            self.filterClicked1()
    def filterClicked1(self):
        print "searching logic"
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    tb = mtable()
    sys.exit(app.exec_())

在这里,我需要使用箭头键选择绿色突出显示的项目,而无需单击该项目

enter image description here

1 个答案:

答案 0 :(得分:2)

您必须使用QListView的currentChanged的{​​{1}}信号:

selectionModel()