[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)
        self.listView.setModel(model)
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

            item.setCheckState(check)
            item.setCheckable(True)

            model.appendRow(item)

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

        self.model = model

        self.resize(300, 240)

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

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

    def contextMenuEvent(self, event):
        self.contextMenu.exec_(event.globalPos())
##

    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()
    view.show()
    app.exec_()


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