I want to make checked Items in Bold Format in QT DOM Model. For the full code please follow the Git link. https://github.com/aviralarpit/QTreeView_with_XML
QVariant DomModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
DomItem *item = static_cast<DomItem*>(index.internalPointer());
const QDomNode node = item->node();
if ( role == Qt::CheckStateRole && (index.column() == 0) && hasChildren(index) )
return static_cast< int >( item->isChecked() ? Qt::Checked : Qt::Unchecked );
if (role != Qt::DisplayRole)
return QVariant();
switch (index.column()) {
case 0:
return node.nodeName();
case 1:
{
const QDomNamedNodeMap attributeMap = node.attributes();
QStringList attributes;
for (int i = 0; i < attributeMap.count(); i) {
QDomNode attribute = attributeMap.item(i);
attributes << attribute.nodeName() "=\""
attribute.nodeValue() '"';
}
return attributes.join(' ');
}
case 2:
return node.nodeValue().split('\n').join(' ');
default:
break;
}
return item->data(index.column());
}
Flag function
Qt::ItemFlags DomModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if ( index.column() == 0 )
flags |= Qt::ItemIsUserCheckable;
return flags;
}
setData function
bool DomModel::setData(const QModelIndex &index, const QVariant &value, int role) {
DomItem *item = static_cast<DomItem*>(index.internalPointer());
if (index.column() == 0 ){
if (role == Qt::EditRole) {
return false;
}
if (role == Qt::CheckStateRole) {
item->setChecked(value.toBool());
emit dataChanged(index, index);
return true;
}
}
return QAbstractItemModel::setData(index, value, role);
}
CodePudding user response:
In your data()
function add a case for the Qt::FontRole
item data role. Then just return a QFont
instance that looks whathever you want it to look like, depending on the item's check state.
if (role == Qt::FontRole && item->isChecked()) {
QFont font;
font.setBold(true);
return font;
}