Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Context menu on tree view
Forum Update on Monday, May 27th 2025

Context menu on tree view

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 10.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indrajeet
    wrote on 21 May 2012, 11:19 last edited by
    #1

    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.

    @QTreeView *view = new QTreeView(dock);

    view->setModel(model);
    view->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(view,SIGNAL(customContextMenuRequested(const QPoint &)),this,SLOT(contextualMenu(const QPoint &)));

    dock->setWidget(view);
    addDockWidget(Qt::LeftDockWidgetArea, dock);
    void MainWindow::contextualMenu(const QPoint& point)
    {
    QMenu *menu = new QMenu;
    QModelIndex index = view->currentIndex();

    QString fileName = model->data(model->index(index.row(), 0),0).toString();
    menu->addAction(QString("Import"), this, SLOT(test_slot()));
    menu->addAction(QString("Export"), this, SLOT(test_slot()));
    menu->exec(QCursor::pos());
    }
    @

    BR
    Indrajeet

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on 21 May 2012, 11:27 last edited by
      #2

      How about:

      @
      void MainWindow::contextualMenu(const QPoint& point)
      {
      QModelIndex index = view->currentIndex();

      if(view->rootIndex() == index)
      {
      QMenu *menu = new QMenu(view);
      QString fileName = model->data(model->index(index.row(), 0),0).toString();
      menu->addAction(QString("Import"), this, SLOT(test_slot()));
      menu->addAction(QString("Export"), this, SLOT(test_slot()));
      menu->exec(QCursor::pos());
      }
      }
      @

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Indrajeet
        wrote on 21 May 2012, 11:33 last edited by
        #3

        Hi

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

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Indrajeet
          wrote on 21 May 2012, 11:55 last edited by
          #4

          Hi

          I want to do something like this
          @
          Root Node
          |-Child1
          |-Child2
          |-Child3
          @

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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on 21 May 2012, 12:20 last edited by
            #5

            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":https://qt-project.org/doc/qt-4.8/qabstractitemview.html#setRootIndex . But there's only one root item in a view.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              Indrajeet
              wrote on 21 May 2012, 12:50 last edited by
              #6

              Hi

              I didnt get you.

              Below is my tree view structure

              @
              Root Node
              |-Child1
              |-1
              |-2
              |-Child2
              |-1
              |-2
              |-Child3
              |-1
              |-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

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on 21 May 2012, 13:13 last edited by
                #7

                I think you want to have a behaviour like this:
                @
                void MainWindow::contextualMenu(const QPoint& point)
                {
                QModelIndex index = view->currentIndex();

                if(view->model()->hasChildren(index))
                {
                  QMenu *menu = new QMenu(view);
                  QString fileName = model->data(model->index(index.row(), 0),0).toString();
                  menu->addAction(QString("Import"), this, SLOT(test_slot()));
                  menu->addAction(QString("Export"), this, SLOT(test_slot()));
                  menu->exec(QCursor::pos());
                }
                

                }
                @

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Indrajeet
                  wrote on 22 May 2012, 09:19 last edited by
                  #8

                  Hi

                  Thanx for you reply.

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

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KA51O
                    wrote on 22 May 2012, 09:34 last edited by
                    #9

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

                    @
                    void MainWindow::contextualMenu(const QPoint& point)
                    {
                    QModelIndex index = view->currentIndex();

                        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
                           }
                        }
                    }
                    

                    @

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      Indrajeet
                      wrote on 22 May 2012, 10:05 last edited by
                      #10

                      Hi

                      In my case if i right click on root node it will not enter the below if condition
                      @
                      QModelIndex index = view->currentIndex();
                      if(view->rootIndex() == index)
                      {
                      }
                      @

                      And how to check for child nodes?

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        KA51O
                        wrote on 22 May 2012, 10:39 last edited by
                        #11

                        [quote author="Rajveer" date="1337681154"]Hi

                        In my case if i right click on root node it will not enter the below if condition
                        @
                        QModelIndex index = view->currentIndex();
                        if(view->rootIndex() == index)
                        {
                        }
                        @

                        And how to check for child nodes?[/quote]

                        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.

                        1 Reply Last reply
                        0

                        1/11

                        21 May 2012, 11:19

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved