如何使用pyqt4在列表视图的匹配项中获取所选项

时间:2019-03-20 12:11:16

标签: python-2.7 pyqt4

这是我的示例代码。我正在学习列表视图方法,我已经发布了一个问题,但是我对程序有一个小疑问。在我的程序中,“ fliter”之后我得到了单词中的匹配项我想使用回车键选择所选的项目,但是选择后它正在打印第一项。.我不想打印匹配列表的第一项。.有谁能帮助我吗?谢谢

以下是我的代码:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent=parent)
        vLayout = QVBoxLayout(self)
        hLayout = QHBoxLayout()

        self.lineEdit = QLineEdit(self)
        hLayout.addWidget(self.lineEdit)

        self.filter = QPushButton("filter", self)
        hLayout.addWidget(self.filter)
        self.filter.clicked.connect(self.filterClicked)

        self.list = QListView(self)

        vLayout.addLayout(hLayout)
        vLayout.addWidget(self.list)

        self.model = QStandardItemModel(self.list)

        codes = [
            'windows',
            'windows xp',
            'windows7',
            'hai',
            'habit',
            'hack',
            'good'
        ]
        for code in codes:
            item = QStandardItem(code)
            self.model.appendRow(item)
        self.list.setModel(self.model)
        shorcut=QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return), self)
        shorcut.activated.connect(self.on_enter_pressed)
    @QtCore.pyqtSlot()
    def on_enter_pressed(self):
        if len(self.lineEdit.text())>0:
            self.filterClicked()

    def filterClicked(self):
        filter_text = str(self.lineEdit.text()).lower()
        for row in range(self.model.rowCount()):
            if filter_text in str(self.model.item(row).text()).lower():
                self.list.setRowHidden(row, False)
                self.list.setFocus()
            else:
                self.list.setRowHidden(row, True)

        ix = self.list.selectionModel().selectedIndexes()
        #here if i mentioned self.list.selectionModel().currentIndex() means it is automatically printing the first item in List_View
        # i dont want to print first item ...after slecting the item in list view i will press enter key then only i want to print the selected item name
        print ix.data()
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

终于我得到了这个答案。.thqeyllanesc先生,我参考了您以前与列表视图有关的所有答案。。非常感谢您。

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent=parent)
        vLayout = QVBoxLayout(self)
        hLayout = QHBoxLayout()

        self.lineEdit = QLineEdit(self)
        hLayout.addWidget(self.lineEdit)

        self.filter = QPushButton("filter", self)
        hLayout.addWidget(self.filter)
        self.filter.clicked.connect(self.filterClicked)

        self.list = QListView(self)

        vLayout.addLayout(hLayout)
        vLayout.addWidget(self.list)

        self.model = QStandardItemModel(self.list)

        codes = [
            'windows',
            'windows xp',
            'windows7',
            'hai',
            'habit',
            'hack',
            'good'
        ]
        for code in codes:
            item = QStandardItem(code)
            self.model.appendRow(item)
        self.list.setModel(self.model)
        shorcut=QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return), self)
        shorcut.activated.connect(self.on_enter_pressed)
    @QtCore.pyqtSlot()
    def on_enter_pressed(self):
        if len(self.lineEdit.text())>0:
            self.filterClicked()

    def filterClicked(self):
        filter_text = str(self.lineEdit.text()).lower()
        for row in range(self.model.rowCount()):
            if filter_text in str(self.model.item(row).text()).lower():
                self.list.setRowHidden(row, False)
                self.list.setFocus()
            else:
                self.list.setRowHidden(row, True)
        indexes =  self.list.selectionModel().selectedIndexes()
        for index in indexes:
            print index.data().toString()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())