从QListView PyQt中检索所选项目

时间:2014-01-23 15:50:25

标签: python python-2.7 pyqt pyqt4

这是一个在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_())

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

3 个答案:

答案 0 :(得分:3)

将此添加到您的MyForm课程:

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

你也可以知道行的编号:

    row_number = model_index.row()

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

答案 1 :(得分:3)

正如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

答案 2 :(得分:2)

替换

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)))