Hello, can someone teach me how to solve this problem? <br>I first created a QListView with the following two lines.<br> self.listView = QListView(self)<br>
self.listView.setModel(model)<br>Afterwards, I hope to get the currentItem with "print self.listView.currentItem().text()", but this does not work. The error says:<br>''AttributError; QListView object has no attribute 'currentItem'<br>
How could I solve this? It seems that I could not use any of the members of QListView. Thanks. <br>My code is below:<br><br><br># -*- coding: utf-8 -*-<br>from PyQt4.QtCore import *<br>from PyQt4.QtGui import *<br>from PyQt4 import QtCore, QtGui<br>
import sys<br>from random import randint<br><br>class View(QWidget):<br> def __init__(self, parent=None):<br> super(View, self).__init__(parent)<br><br><br> model = QStandardItemModel()<br><br> for n in range(10): <br>
item = QStandardItem('Item %s' % randint(1, 100))<br><br> check = Qt.Checked if randint(0, 1) == 1 else Qt.Unchecked<br><br> item.setCheckState(check)<br> item.setCheckable(True)<br>
<br> model.appendRow(item)<br><br> model.connect(model, SIGNAL("itemChanged(QStandardItem *)"), self.itemChanged)<br> self.listView = QListView(self)<br> self.listView.setModel(model)<br>
<br> self.model = model<br> <br> self.resize(300, 240)<br><br> self.contextMenu = QtGui.QMenu(self)<br> action = QtGui.QAction('Current Item', self)<br> self.connect(action, QtCore.SIGNAL("triggered()"), self.showCurrentItem)<br>
self.contextMenu.addAction(action)<br> <br> def showCurrentItem(self):<br>## print self.listView.columns()<br> print self.listView.currentItem().text()<br> <br> def contextMenuEvent(self, event):<br>
self.contextMenu.exec_(event.globalPos())<br>## <br> <br> def itemChanged(self, item):<br> for row in range(self.model.rowCount()):<br> item = self.model.item(row, 0)<br> if item.checkState() == Qt.Checked:<br>
print item.text(), 'is selected'<br> print '--------------------------------'<br><br>if __name__ == '__main__':<br> app = QApplication(sys.argv)<br> view = View()<br> view.show()<br>
app.exec_()<br><br><br clear="all">---------------------------<br>He Jibo<br>Department of Psychology,<br>Beckman Institute for Advanced Science and Technology<br>University of Illinois, Urbana Champaign,<br>603 East Daniel St.,<br>
Champaign, IL 61820<br>website: <a href="http://www.hejibo.info">www.hejibo.info</a><br><br>