[PyQt] [help]QListView object has no attribute 'currentItem'

He Jibo hejibo at gmail.com
Wed Jun 30 15:34:37 BST 2010

Hello, can someone teach me how to solve this problem?
I first created a QListView with the following two lines.
        self.listView = QListView(self)
Afterwards, I hope to get the currentItem with "print
self.listView.currentItem().text()", but this does not work. The error says:
''AttributError; QListView object has no attribute 'currentItem'
How could I solve this? It seems that I  could  not use any of the members
of QListView. Thanks.
My code is below:

# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
import sys
from random import randint

class View(QWidget):
    def __init__(self, parent=None):
        super(View, self).__init__(parent)

        model = QStandardItemModel()

        for n in range(10):
            item = QStandardItem('Item %s' % randint(1, 100))

            check = Qt.Checked if randint(0, 1) == 1 else Qt.Unchecked



        model.connect(model, SIGNAL("itemChanged(QStandardItem *)"),
        self.listView = QListView(self)

        self.model = model

        self.resize(300, 240)

        self.contextMenu = QtGui.QMenu(self)
        action = QtGui.QAction('Current Item', self)
        self.connect(action, QtCore.SIGNAL("triggered()"),

    def showCurrentItem(self):
##        print self.listView.columns()
        print self.listView.currentItem().text()

    def contextMenuEvent(self, event):

    def itemChanged(self, item):
        for row in range(self.model.rowCount()):
            item = self.model.item(row, 0)
            if item.checkState() == Qt.Checked:
                print item.text(), 'is selected'
        print '--------------------------------'

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = View()

He Jibo
Department of Psychology,
Beckman Institute for Advanced Science and Technology
University of Illinois, Urbana Champaign,
603 East Daniel St.,
Champaign, IL 61820
website: www.hejibo.info
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100630/1d476010/attachment.html>

More information about the PyQt mailing list