views:

2455

answers:

2

Hello,

I want to display a QListView where each item is a checkbox with some label. The checkboxes should be visible at all times. One way I can think of is using a custom delegate and QAbstractListModel. Are there simpler ways? Can you provide the simplest snippet that does this?

Thanks in advance

+3  A: 

See my reply to your question on the PyQt mailing list:

http://www.riverbankcomputing.com/pipermail/pyqt/2009-May/023002.html

Summary: You need to use the ItemIsUserCheckable flag and CheckStateRole role.

David Boddie
+2  A: 

I ended up using the method provided by David Boddie in the PyQt mailing list. Here's a working snippet based on his code:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from random import randint


app = QApplication(sys.argv)

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)


view = QListView()
view.setModel(model)

view.show()
app.exec_()

Note: changed the call of setData with a check role to setCheckState and used setCheckable instead of flags.

Eli Bendersky