How to make text Bold of the Item selected with Checkbox in QT?
-
I want to make checked Items in Bold Format in QT DOM Model.
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); }
I need Fonts bold of the items which have checkbox is checked! -
I want to make checked Items in Bold Format in QT DOM Model.
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); }
I need Fonts bold of the items which have checkbox is checked! -
@Aviral-0 said in How to make text Bold of the Item selected with Checkbox in QT?:
... && hasChildren(index)
Wow!
-
@JonB yes it solved the problem, simple and easy, not complicating things. You can see the result in image
@Aviral-0 said in How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.:
@JonB You don't know anything, just arrogant rude. I didn't find any solutions to my problem from you!
So precisely what I explained to you to do.
-
@Aviral-0 said in How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.:
@JonB You don't know anything, just arrogant rude. I didn't find any solutions to my problem from you!
So precisely what I explained to you to do.
-
I want to make checked Items in Bold Format in QT DOM Model.
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); }
I need Fonts bold of the items which have checkbox is checked! -
@Aviral-0
So far i have tried to Implement
if (role == Qt::FontRole && item->isChecked())
return QFont::Bold;
But its not working. Anyone Please help