PDA

View Full Version : Updating checkboxes in QTreeView programmatically



Jeffb
18th June 2010, 06:07
Hi All

I have a QTreeView with a custom model.
The behaviour I'm looking for is that a user can check or uncheck bottom level items (ie. items that have no children) and when all the siblings have been checked for a particular parent, the parent gets checked.

So far I have no problem with getting the correct bottom level items to check and uncheck but the parent won't update until either it is selected or the QTreeView loses focus.

The reimplemented code for the flags is:


else if (index.column() == columnHeadingsList.indexOf("Complete"))
{
if ((aNode->children).count() == 0)
{
flags |= Qt::ItemIsUserCheckable;
}
}

The code for data is:

else if (role == Qt::CheckStateRole)
{
if (index.column() == columnHeadingsList.indexOf("Complete"))
{
if (aNode->getIsComplete())
return Qt::Checked;
else
return Qt::Unchecked;
}
}

And the code for setData is:

else if (index.column() == columnHeadingsList.indexOf("Complete"))
{
if (role == Qt::CheckStateRole)
{
// set this node's isComplete
aNode->setIsComplete(value.toBool());

// set parent node's isComplete depending on it's children being complete
JBModelRow* aParentNode = aNode->parent;
if (aParentNode)
{
bool aNodeComplete = true;
for (int i = 0; i < (aParentNode->children).count(); i++)
{
if (!aParentNode->children[i]->getIsComplete())
{
aNodeComplete = false;
i = (aParentNode->children).count();
}
}
aParentNode->setIsComplete(aNodeComplete);

QModelIndex parentIndex = createIndex(aParentNode->getRow(),
aParentNode->getCol(),
aParentNode);

emit dataChanged(parentIndex, parentIndex);
}
}
}

Any help appreciated.

Jeff

Jeffb
18th June 2010, 13:06
Have worked out what was wrong.
I had the wrong column when creating the QModelIndex parentIndex. It should have been the index.col

Cheers
Jeff