【问题标题】:Retrieve selected item from QListView PyQt从 QListView PyQt 中检索选定的项目
【发布时间】:2014-01-23 15:50:24
【问题描述】:

这是一个在 stackoverflow 上被问过很多次的问题,我已经解决了所有这些问题,但它们似乎并没有解决问题。我只想知道在 QListView 上单击了哪个项目。

这是我正在尝试的代码。

from PyQt4 import QtCore, QtGui

class MyModel(QtCore.QAbstractListModel):
    def __init__(self,data=[],parent=None):
        QtCore.QAbstractListModel.__init__(self,parent)
        self._data=data

    def rowCount(self,parent):
        return len(self._data)

    def data(self,index,role):

        if role==QtCore.Qt.DisplayRole:
            return self._data[index.row()]


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(640, 480)
        self.verticalLayout = QtGui.QVBoxLayout(Form)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.listView = QtGui.QListView(Form)
        self.listView.setObjectName(_fromUtf8("listView"))

        self.verticalLayout.addWidget(self.listView)
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.verticalLayout.addWidget(self.lineEdit)
        data=["one","two","three","four"]
        model=MyModel(data)
        self.listView.setModel(model)
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
        QtCore.QObject.connect(self.listView ,     QtCore.SIGNAL(_fromUtf8("listclicked()")),self.PrintIT)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))

    def PrintIT(self,selected):
        print "Asdf"
        self.lineEdit.text(str(self.listView.selectedItem()))
import sys

class MyForm(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
    def execute_event(self):
        pass
    def execute_all_event(self):
        pass
    def reload_event(self):
        pass

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

我尝试了很多解决方案,但没有一个能解决这个问题。 提前致谢。

【问题讨论】:

    标签: python python-2.7 pyqt pyqt4


    【解决方案1】:

    将此添加到您的 MyForm 课程中:

    @QtCore.pyqtSlot("QModelIndex")
    def on_listView_clicked(self, model_index):
        # Process here the model index.
    

    你也可以知道行号:

        row_number = model_index.row()
    

    另请注意,您使用的是QListView,而不是QListWidget。最后一个有 QListWidgetItem 对象是你正在使用的对象,而不是。

    【讨论】:

    • 您可以,但是当您更新您的 ui 文件并再次使用 pyuic4 时,您的 Ui_Form 类将被覆盖。所以你会失去那个方法。
    • 感谢您的回答,我并没有真正注意到这种连接信号和插槽的方式。
    【解决方案2】:

    正如 RaydelMiranda 所说,不建议在 Ui_Form 类中手动编写代码,因为当使用 Qt Designer 更改 GUI 时,您更改的所有内容都会被覆盖。

    你连接失败的原因是没有信号listclickedQListView 的信号继承自 QAbstractItemView

    void  activated ( const QModelIndex & index )
    void  clicked ( const QModelIndex & index )
    void  doubleClicked ( const QModelIndex & index )
    void  entered ( const QModelIndex & index )
    void  pressed ( const QModelIndex & index )
    void  viewportEntered ()
    

    信号与槽的连接方式应该是:

    self.listView.clicked.connect(self.PrintIT)
    

    或如 RaydelMiranda 的回答所示。 The new style of connecting signals and slots introduced in PyQt4 v4.5 is here

    【讨论】:

      【解决方案3】:

      替换

      QtCore.QObject.connect(self.listView , QtCore.SIGNAL(_fromUtf8("listclicked()")),self.PrintIT)
      

      通过

      self.listView.clicked.connect(self.PrintIT)
      

      然后在self.PrintIT:

      def PrintIT(self,index):
          print "Asdf"
          self.lineEdit.setText(str(self.listView.model().itemData(index)))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多