PDA

View Full Version : Checkboxes in QTableView with my custom model



frankiefrank
11th August 2011, 17:01
I read a few threads on this subject but stayed kind of confused.

I have a custom model and a QTableView, and I'm using a custom class deriving from QItemDelegate for two columns I need widgets in (a QSpinBox like in the Qt example, and a QComboBox).

Now I want to add a checkbox, but unlike the combo and spinbox, where the actual widget becomes displayed only in edit mode, I want my checkboxes to be always displayed. Of course I'll also want to connect to signals emitted from checking/unchecking.

What's the best way to add these checkboxes in my new column?

Santosh Reddy
12th August 2011, 00:12
QTableView can display a checkbox by default, just enable Qt::ItemIsUserCheckable flag in the model in QAbstractItemModel::flags(). For signals when checked, you need to explicitly emit signals (user defined signals) when user checks the item, i.e. from QAbstractItemModel::setData()

Jeffb
12th August 2011, 12:47
You'll also need this in your data(const QModelIndex &index, int role) method:


if (role == Qt::CheckStateRole) // this shows the checkbox
{
bool aBool = fieldValue;
if (aBool)
return Qt::Checked;
else
return Qt::Unchecked;
}

jeff

frankiefrank
15th August 2011, 14:43
Thanks for the help. Still a bit confused about the data() / setData().

My model's data() method for the
Qt::CheckStateRole should return the checked/unchecked status. But based on what do I decide that?
Here's what I mean - I want the checkable status to "represent" a boolean value.
Do I need to store the boolean value "behind the scenes" in a custom user role?

Added after 1 47 minutes:

Right now, I manage to get checkboxes displayed but I can't edit them (can't check).

My flags() implementation adds the following flags to the checkbox column


flags |= Qt::ItemIsUserCheckable;
flags |= Qt::ItemIsSelectable;
flags |= Qt::ItemIsEnabled;


My setData stores a string "true"/"false" in an underlying object representing the current state, only when called for the checkbox column and the check state role.

I put a breakpoint in the setData code relevant to that column and role, but I don't see any calls. Clicks on the checkbox don't cause it to get there.

Jeffb
17th August 2011, 12:35
Here is an example of the setData method in one of my models:


bool JB_NodeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool success = true;

if (index.isValid())
{
// get field name of index
QString fieldName = mapped_dbFieldNames[index.column()];
JB_Node* node = nodeFromIndex(index);

if (role == Qt::CheckStateRole)
{
// Columns with a checkbox must be included here so it can be edited through the checkbox on the QtableView
if (editableCheckBoxDBFieldNamesList.contains(fieldNa me))
{
QVariant boolValue = QVariant(value.toBool());
node->setData(fieldName, boolValue); // this is the important part
QModelIndex topLeft = index;
QModelIndex bottomRight = index;
emit dataChanged(topLeft, bottomRight);
success = true;
}
}
else
{
node->setData(fieldName, value);
QModelIndex topLeft = index;
QModelIndex bottomRight = index;
emit dataChanged(topLeft, bottomRight);
success = true;
}
}
else
success = false;

return success;
}

Added after 5 minutes:

Also

you only need

flags |= Qt::ItemIsUserCheckable;
in your flags method to make your checkboxes checkable.

if you don't want your checkbox columns/fields to be editable, then use

flags |= Qt::ItemIsSelectable;
flags |= Qt::ItemIsEnabled;
for those fields.

ken123
30th January 2012, 20:08
QTableView can display a checkbox by default, just enable Qt::ItemIsUserCheckable flag in the model in QAbstractItemModel::flags(). For signals when checked, you need to explicitly emit signals (user defined signals) when user checks the item, i.e. from QAbstractItemModel::setData()

I need the ability to show checkbox, I am still confuse on this issue. How do I set the flags. QAbstractItemModel::flags() is a getter not a setter, right?