PDA

View Full Version : QSqlTableModel and checkboxes with SQLite database



ibergmark
22nd February 2008, 14:05
Hi,
I just started to use Qt Open Source edition last week.

In my test-project I'm using a QSqlTableModel with a QTableView to display data.
One column has an integer value of 0 or 1 which I want to be displayed as a checkbox in the QTableView. The user should also be able to click on it to check/uncheck.

My database engine is SQLite, and it does not have a boolean column type, so no automatic definition can be made.

I've been browsing the forum and following some examples I've sub-classed the QItemDelegate and used setItemDelegateForColumn(), but this only displays a checkbox after double-clicking for editing.

I want a checkbox to always be displayed in the view.
I haven't re-implemented the paint event yet, but it seems a little awkward to have to re-implement so many functions when there is a Qt::ItemIsUserCheckable flag for the model.

Have I started out all wrong?

Is there a way that I could set the Qt::ItemIsUserCheckable flag in the table model for my column to handle this more or less automatically?

Best Regards,
Ingemar

jpn
22nd February 2008, 23:41
Is there a way that I could set the Qt::ItemIsUserCheckable flag in the table model for my column to handle this more or less automatically?
You can reimplement QAbstractItemModel::flags() to return Qt::ItemIsUserCheckable together with other flags for the checkable column. But notice that you must also handle Qt::CheckStateRole in data() and setData().

ibergmark
23rd February 2008, 01:58
But notice that you must also handle Qt::CheckStateRole in data() and setData().

Aha, that's where I went wrong... I'm going to try this approach also...

Thanks!

/ Ingemar