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;
}
}
else if (index.column() == columnHeadingsList.indexOf("Complete"))
{
if ((aNode->children).count() == 0)
{
flags |= Qt::ItemIsUserCheckable;
}
}
To copy to clipboard, switch view to plain text mode
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;
}
}
else if (role == Qt::CheckStateRole)
{
if (index.column() == columnHeadingsList.indexOf("Complete"))
{
if (aNode->getIsComplete())
return Qt::Checked;
else
return Qt::Unchecked;
}
}
To copy to clipboard, switch view to plain text mode
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);
}
}
}
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);
}
}
}
To copy to clipboard, switch view to plain text mode
Any help appreciated.
Jeff
Bookmarks