2014-01-23 31 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