How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.
-
@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
? -
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 Using
&& index.row() == 0
also didn't worked. I think index contains the current item location in tree -
@Aviral-0
YourQTreeView
has a root index, the one you want a checkbox against, right? How did that get set? You want to know that index in the model code so that you can check for it yourdata()
code, right? -
@Aviral-0
YourQTreeView
has a root index, the one you want a checkbox against, right? How did that get set? You want to know that index in the model code so that you can check for it yourdata()
code, right? -
@JonB Please have a look at it.
https://github.com/aviralarpit/QTDomDoc.git -
@Aviral-0
class DomModel
hasQModelIndex rootIndex() const;
(though can't see an implementation?) orDomItem *rootItem
you can use to identify the root item. -
@JonB I tried to use rootIndex() function but didn't understood how to.
Its just declaration. -
@Aviral-0
class DomModel
hasQModelIndex rootIndex() const;
(though can't see an implementation?) orDomItem *rootItem
you can use to identify the root item. -
@JonB I know its alot to ask, but can you tell me what change should I do specifically to make it work. It will be so nice of you taking your time to help me. Thankyou Brother.
-
@Aviral-0
I'm not your Brother!I think you want in
flags()
:DomItem *item = static_cast<DomItem*>(index.internalPointer()); if (item == rootItem) flags |= Qt::ItemIsUserCheckable;
to make it so only the root item is checkable.
-
@Aviral-0
I'm not your Brother!I think you want in
flags()
:DomItem *item = static_cast<DomItem*>(index.internalPointer()); if (item == rootItem) flags |= Qt::ItemIsUserCheckable;
to make it so only the root item is checkable.
-
@Aviral-0
I'm not your Brother!I think you want in
flags()
:DomItem *item = static_cast<DomItem*>(index.internalPointer()); if (item == rootItem) flags |= Qt::ItemIsUserCheckable;
to make it so only the root item is checkable.
@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 ? -
@JonB I have solved this.
By using
DomModel::data(){
if ( role == Qt::CheckStateRole && !index.parent().isValid())
}@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? -
@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 -
@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
-
@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
-
@Aviral-0
If "nothing worked" that means you are saying your code is not being called at all. I cannot help if you say that is the case. -
@Aviral-0
If "nothing worked" that means you are saying your code is not being called at all. I cannot help if you say that is the case.