How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.
-
@Aviral-0
YourDomModel::data()
method has correct code to look atitem->isChecked()
to see whether to show checked/unchecked. But I do not see anywhere in your code which sets anyDomItem
(or itsQDomNode
returned bynode()
) to record that it has been checked. You have to store that somewhere. And ifDomModel::data()
looks atitem->isChecked()
forCheckStateRole
I think I would expect you to have a correspondingsetData()
with case forCheckStateRole
which calls anitem->setChecked()
. -
@JonB Thankyou, using setData() method the items are now checkable.
Just one thing I am not understanding. you said about
QModelIndex QAbstractItemView::rootIndex() const
I am not able to understand how to use it and where to use it.
as on website its not indepth elaboration about it. -
Qt::ItemFlags DomModel::flags(const QModelIndex &index) const { if ( index.column() == 0 ) flags |= Qt::ItemIsUserCheckable;
You said, I believe, that you only want a checkbox if it is the root index. You want to know whether
index
is the root index here. I realize now that QModelIndex QAbstractItemView::rootIndex() const is a method of theQTreeView
and not available in model flags code. Is the root index row 0 in the model (I don't know)? So you would need maybe&& index.row() == 0
? -
@JonB Hi Jon, just one more thing needed.
I want to Highlight the items of tree which is selected, like when we do multiple selection using CTRL button, the selected lines should be in blue highlight or their fonts can done Bold to make selected Items looks different.
How should I do it? any idea ? -
@Aviral-0 said in How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.:
&& !index.parent().isValid()
Ah, well done! I kind of thought that was for a child of the root element, and the root element was that parent. But I think it makes sense, I haven't actually ever used
QTreeView
I don't think.Do you mean you only want to alter presentation when multiple items are selected, not just a single one? Normally you would do colour/font via stylesheet on selected items. But that won't distinguish multiple versus single selection.
QTreeView::selectionModel
/selectedIndexes()
gives you which items/indexes are selected, that's all I know, somehow work from there? -
@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