views:

566

answers:

3

Hi all,

I want to retrieve a list of people's names from a queue and, for each person, place a checkbox with their name to a QGridLayout using the addWidget() function. I can successfully place the items in a QListView, but they just write over the top of each other rather than creating a new row. Does anyone have any thoughts on how I could fix this?

self.chk_People = QtGui.QListView()
items = self.jobQueue.getPeopleOffQueue()

for item in items:
    QtGui.QCheckBox('%s' % item, self.chk_People)

self.jobQueue.getPeopleOffQueue() would return something like ['Bob', 'Sally', 'Jimmy'] if that helps.

A: 

I can't tell you the solution in PyQt but the structure you need to follow is the following. The QListView can do what you need, you don't need to create separate checkboxes. Create a subclass of QAbstractItemModel or QStandardItemModel (depending on how much coding you will want to do) override the flags() method to return the appropriate flags including Qt::ItemIsUserCheckable, in the columncount() method add an extra column to include the checkbox and in the data method for the column where you want your checkbox to appear return the checked state Qt::Checked, Qt::Unchecked for the Qt::CheckStateRole.

This can also be accomplished using the QListWidget where you can use QListWidgetItem for adding data and do not need to create a model. On QListWidgetItem you can use setFlags() and setData(QVariant(bool, Qt::CheckStateRole) without having to subclass a model

Harald Scheirich
+1  A: 

This line:

QtGui.QCheckBox('%s' % item, self.chk_People)

Doesn't add the check box to the list view, it only creates it with the list view as the parent, and there's a big difference.

The simplest way to use a list view is the QListWidget convenience class. For that, create your checkboxes as instances of QListWidgetItem and then use addItem on the list widget to really add them to it.

Did you have a problem adding them to a grid layout? Usually, if the amount of checkboxes you have is small a grid layout might be better - it all depends on how you want your app to look. But if you might have a lot of such objects, then a list widget/view is the best.

Eli Bendersky
worked great thanks
A: 

Hi everyone:

I'm just a PyQt4 beginner and I woould like knowing how to set a different icon on a QMessageBox. I mean an icon for the window.

Thanx. May God Bless you all.

carlos