Hi guys,
I'm currently solving almost the same problem, especially the following one:

Originally Posted by
jpn
I wonder how did you implement the QStandardItem::setData method ? It looks that the only way to do that is
- Go through all items in the model and un-check them
- Run the default implementation for the being checked item so that the new state is properly processed
So something like this:
{
for (int i = 0; i < parent_item->rowCount(); ++i)
{
if (Qt::Checked == child_item->checkState())
{
child_item->setCheckState(Qt::Unchecked);
}
uncheckBranchItems(child_item);
}
}
void RadioButtonItem
::setData(const QVariant & value,
int role
) {
if (Qt::CheckStateRole == role)
{
if (value.isValid() && (Qt::Checked == static_cast<Qt::CheckState>(value.toInt())))
{
uncheckBranchItems(model()->invisibleRootItem());
}
}
}
void uncheckBranchItems(QStandardItem * parent_item)
{
for (int i = 0; i < parent_item->rowCount(); ++i)
{
QStandardItem * child_item = parent_item->child(i);
if (Qt::Checked == child_item->checkState())
{
child_item->setCheckState(Qt::Unchecked);
}
uncheckBranchItems(child_item);
}
}
void RadioButtonItem::setData(const QVariant & value, int role)
{
if (Qt::CheckStateRole == role)
{
if (value.isValid() && (Qt::Checked == static_cast<Qt::CheckState>(value.toInt())))
{
uncheckBranchItems(model()->invisibleRootItem());
}
}
QStandardItem::setData(value, role);
}
To copy to clipboard, switch view to plain text mode
Now imagine that I have another object which subscribes to the QTreeView::clicked() signal, check the item check state and do some custom logic then. I need somehow emulate the signal for the items being un-checked during call to uncheckBranchItems(). The signal should be emitted from the model, not from the item. Because possible subscribers to the signal do not know anything about the items but about model only. How would you do that ?
I'm actually curious what was your implementation of QStandardItem::setData method and whether you implemented some signal notifications on current checked item changes. Probably my solution is bad and you could advise something better where the required signal notifications are easy to do?
Bookmarks