PDA

View Full Version : checkbox on QListView



ken123
31st January 2012, 14:51
Is it possible to turn on or use checkbox as one of my listItem? For example,
I have a list:
item1
item2
item3
...
I want to have the ability to set or unset the checkbox for all items (checkbox and text of the item on the column). I use the QStringListModel to handle the view now. How can I add this feature to my model to show up on my QListView? Thanks.

Jonny174
1st February 2012, 09:26
QListWidgetItem *item = new QListWidgetItem;
item->setData( Qt::DisplayRole, "text" );
item->setData( Qt::CheckStateRole, Qt::Checked );
listWidget->addItem( item );




QStandardItemModel *model = new QStandardItemModel();
QStandardItem *Item = new QStandardItem();
Item->setCheckable( true );
Item->setCheckState( Qt::Checked );
model->setItem( 0, Item );
listView->setModel( model );

LeeMinh
18th July 2013, 12:00
@Jonny174: Thanks for your useful code! It is really helpful. But how can I know what the checkbox is checked after I added checkbox for all items in QListView?

Thanks!




QListWidgetItem *item = new QListWidgetItem;
item->setData( Qt::DisplayRole, "text" );
item->setData( Qt::CheckStateRole, Qt::Checked );
listWidget->addItem( item );




QStandardItemModel *model = new QStandardItemModel();
QStandardItem *Item = new QStandardItem();
Item->setCheckable( true );
Item->setCheckState( Qt::Checked );
model->setItem( 0, Item );
listView->setModel( model );