How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.
-
@JonB I am trying this piece of code:
const bool shouldBeBold = (index.column() == 0);
if (role == Qt::FontRole && shouldBeBold) {
QFont boldFont;
boldFont.setBold(true);
return boldFont;
} else {
return DomModel::data(index, role);
}you repied to this code in past at someone elses pproblem.
but its not working -
@Aviral-0
Debugging techniques:- Put a
qDebug()
into yourif
statement, does it get hit at all? - Set
shouldBeBold = true
unconditionally, does any items come up in bold? - Try, say,
role == Qt::ForegroundRole
andreturn QColor(Qt::red)
, does that work in case it's some bold issue? - If you are doing this on selected items it might be that selection sets font instead, I don't know.
- Put a
-
Hi, Hope you are having a good day.
Now I need the checkboxes to all the parents of child.
I have attached the photo attached of what I need.
What to change in the if condition to make it happen?
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.parent().isValid()) //if ( role == Qt::CheckStateRole && index.column() == 0 && !index.parent().isValid()) if ( role == Qt::CheckStateRole && index.column() == 0 && !index.parent().isValid() ) { 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()); }
-
@Aviral-0
Then upload an image to your post (best, the post toolbar has "cloud" icon tooltip Upload Image), or at worst given a link to where the image can be seen. If you think I am going to download an image you want me to look at from a site , I am not. -
-
@JonB I have tried it bro... I tried removing !index.parent.isvalid()
and placing some other functions.
I am not fully understanding of what you are suggesting. can you tell me what to write there?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.parent().isValid()) //if ( role == Qt::CheckStateRole && index.column() == 0 ) if ( role == Qt::CheckStateRole && (index.column() == 0) ) { 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()); } 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; } //! [6]
-
@Aviral-0 said in How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.:
I didn't find any solutions to my problem from you!
What about "You need to replace the original && !index.parent().isValid() with the condition for QDomNode node not having any children." from @JonB - did you do that?
Please state polite...