May 21, 2012

Adi Adi
Lab Rat
206 posts

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.

  1. QTreeView *view = new QTreeView(dock);
  2.  
  3.  view->setModel(model);
  4.  view->setContextMenuPolicy(Qt::CustomContextMenu);
  5.  connect(view,SIGNAL(customContextMenuRequested(const QPoint &)),this,SLOT(contextualMenu(const QPoint &)));
  6.  
  7.  dock->setWidget(view);
  8.  addDockWidget(Qt::LeftDockWidgetArea, dock);
  9. void MainWindow::contextualMenu(const QPoint& point)
  10. {    
  11.  QMenu *menu = new QMenu;
  12.  QModelIndex index = view->currentIndex();
  13.  
  14.  QString fileName = model->data(model->index(index.row(), 0),0).toString();
  15.  menu->addAction(QString("Import"), this, SLOT(test_slot()));
  16.  menu->addAction(QString("Export"), this, SLOT(test_slot()));
  17.  menu->exec(QCursor::pos());
  18. }

BR
Indrajeet

10 replies

May 21, 2012

KA51O KA51O
Robot Herder
380 posts

How about:

  1. void MainWindow::contextualMenu(const QPoint& point)
  2. {    
  3.  QModelIndex index = view->currentIndex();
  4.  
  5.  if(view->rootIndex() == index)
  6.  {
  7.   QMenu *menu = new QMenu(view);
  8.   QString fileName = model->data(model->index(index.row(), 0),0).toString();
  9.   menu->addAction(QString("Import"), this, SLOT(test_slot()));
  10.   menu->addAction(QString("Export"), this, SLOT(test_slot()));
  11.   menu->exec(QCursor::pos());
  12.  }
  13. }

May 21, 2012

Adi Adi
Lab Rat
206 posts

Hi

I added that if condition but it is not going inside that if condition.
How to add the root element.

May 21, 2012

Adi Adi
Lab Rat
206 posts

Hi

I want to do something like this

  1. Root Node
  2.   |-Child1
  3.   |-Child2
  4.   |-Child3

So I want to set context menu to Root Node only.

May 21, 2012

KA51O KA51O
Robot Herder
380 posts

If by Root Node you mean an item that has children you should instead check for that condition.

  1. if(model->hasChildren(index))
  2. {}

To set the root index use the setRootIndex method [qt-project.org] . But there’s only one root item in a view.

May 21, 2012

Adi Adi
Lab Rat
206 posts

Hi

I didnt get you.

Below is my tree view structure

  1. Root Node
  2.   |-Child1
  3.       |-1
  4.       |-2
  5.   |-Child2
  6.       |-1
  7.       |-2
  8.   |-Child3
  9.       |-1
  10.       |-2

And now in my case context menu is coming for all above nodes.
I want it to restrict it to only Root Node,Child1,Child2,Child3

May 21, 2012

KA51O KA51O
Robot Herder
380 posts

I think you want to have a behaviour like this:

  1. void MainWindow::contextualMenu(const QPoint& point)
  2. {    
  3.     QModelIndex index = view->currentIndex();
  4.      
  5.     if(view->model()->hasChildren(index))
  6.     {
  7.       QMenu *menu = new QMenu(view);
  8.       QString fileName = model->data(model->index(index.row(), 0),0).toString();
  9.       menu->addAction(QString("Import"), this, SLOT(test_slot()));
  10.       menu->addAction(QString("Export"), this, SLOT(test_slot()));
  11.       menu->exec(QCursor::pos());
  12.     }
  13. }

May 22, 2012

Adi Adi
Lab Rat
206 posts

Hi

Thanx for you reply.

But what if we want one context menu for Root Node & different one for Child1,Child2,Child3

May 22, 2012

KA51O KA51O
Robot Herder
380 posts

Well then you have to check for the two different cases and create the menu depending on the result of the check.

  1.     void MainWindow::contextualMenu(const QPoint& point)
  2.     {    
  3.         QModelIndex index = view->currentIndex();
  4.          
  5.         if(view->rootIndex() == index /*check if index is that of the RootNode item*/)
  6.         {
  7.            // construct the context menu required for ChildItem items
  8.         }
  9.         else
  10.         {
  11.            if(view->model()->hasChildren(index) /*check if the index is that of a ChildItem item*/)
  12.            {
  13.                // construct the context menu required for the RootNode item
  14.            }
  15.         }
  16.     }

May 22, 2012

Adi Adi
Lab Rat
206 posts

Hi

In my case if i right click on root node it will not enter the below if condition

  1.  
  2.   QModelIndex index = view->currentIndex();
  3.    if(view->rootIndex() == index)
  4.   {
  5.   }

And how to check for child nodes?

May 22, 2012

KA51O KA51O
Robot Herder
380 posts

Rajveer wrote:
Hi

In my case if i right click on root node it will not enter the below if condition

  1.  
  2.   QModelIndex index = view->currentIndex();
  3.    if(view->rootIndex() == index)
  4.   {
  5.   }

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.

 
  ‹‹ Mixing C and C++/Qt code      [Solved] How to get the list of functions in a dll -Unit testing ››

You must log in to post a reply. Not a member yet? Register here!