2014-01-23 29 views
2

这是一个在stackoverflow上已经被问及很多时间的问题,并且我已经通过了所有这些问题,但他们似乎没有解决问题。我只想知道在QListView上点击了哪个项目。从QListView中检索所选项PyQt

这是我正在尝试的代码。

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

添加到您的MyForm类:

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

你也可以知道行号:

row_number = model_index.row() 

还要注意你与QListView不是QListWidget工作。最后有QListWidgetItem对象是你正在使用的对象,而不是。

+2

你可以,但是当你更新你的UI文件并再次使用'pyuic4'时,你的Ui_Form类将被覆盖。所以你会失去这种方法。 –

+0

谢谢,它工作。 – TheCreator232

+0

感谢您的回答,我没有真正注意到这种连接信号和插槽的方式。 – Frodon

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