Context menu on tree view
Hi All
I was using the Qt Sample Application which shows how to use dock widgets.
This is the sample example which comes when we install QT in below mentioned path
Qt\4.7.4\examples\mainwindows\dockwidgets
To this example i modified to add a treeview on left hand side & textedit on left hand side.
Now I added context menu to treeview.
My Query:
Now the context menu is working for all nodes I want to restrict the context menu only for root node.
It should not appear for other nodes.
- view->setModel(model);
- dock->setWidget(view);
- {
- }
BR
Indrajeet
10 replies
If by Root Node you mean an item that has children you should instead check for that condition.
- if(model->hasChildren(index))
- {}
To set the root index use the setRootIndex method [qt-project.org] . But there’s only one root item in a view.
Well then you have to check for the two different cases and create the menu depending on the result of the check.
- {
- if(view->rootIndex() == index /*check if index is that of the RootNode item*/)
- {
- // construct the context menu required for ChildItem items
- }
- else
- {
- if(view->model()->hasChildren(index) /*check if the index is that of a ChildItem item*/)
- {
- // construct the context menu required for the RootNode item
- }
- }
- }
HiIn my case if i right click on root node it will not enter the below if condition
if(view->rootIndex() == index) { }And how to check for child nodes?
Well if you haven’t set a root node, then of course the check performed by the first if condition will return false. And if you care to read my last post you will see that I already told you how to check for child nodes.
You might want to put some effort into this yourself.
You must log in to post a reply. Not a member yet? Register here!
