You don't have to put them in a separate column. Place them alongside the actual data in column 0. For this to work you need to reimplement QAbstractItemModel::flags() to return ItemIsUserCheckable among other flags for indexes having column()==0. Then you need to return true or false from the data() method of the model for the CheckStateRole and allow the model to be editable by allowing setData() to change the CheckStateRole of your model. The rest will be taken care of automatically.
No, this is wrong. You don't return widgets from the model.So, if i use the following code in the data() function of the model, will it work?
Qt Code:
if(index.column() == 3) // 4th column for checkboxes { if (role == Qt::DisplayRole) { QCheckBox *cbox; return cbox; } } //....... }To copy to clipboard, switch view to plain text mode
It's the same here - you need to set appropriate attributes. There is no real checkbox anywhere, it is mimicked by the item delegate.i am not sure about it. In QTableWidget, we can just set the checkstate property to get a checkbox but in QTableView, i am not sure if i have to create a QCheckBox or not.
You don't. There is no need to.And how will i get the events for the checkbox?
Connect to the dataChanged() signal of the model and check the CheckStateRole of the appropriate index - it will either be true or false depending on the state of the "checkbox" (remember there is no real checkbox there although you see one).Basically i want to get the row number or track name when user clicks on a checkbox. So, whenever user selects/deselects a checkbox, i need to get the track name and add/delete it from the playlist.
You won't - you will display the checkbox in the first column. It looks better this way.Also, since from the database i'll get only 3 columns, how will i add another column to the model without disturbing the database? I am using model->insertColumn() in the code which i posted in my first post.
You already are using delegates. It's only a matter of how much control you want to have over the mechanism of displaying your data in views. The standard delegate takes the DisplayRole and renders it to the target widget with DecorationRole used as the icon. If you want something else, you subclass the default delegate and extend/substitute the painting routine.I am not familiar with delegates, so the solution posted by @calmspeaker looks ok to me. I don't need to modify the database from the GUI, so i can avoid using the delegates.
There is more than one solution for most problems. Choose the one you are most comfortable with.Again, can i use the solution posted by @calmspeaker?
Bookmarks