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. How to add Action specific index on Qtreeview ?

How to add Action specific index on Qtreeview ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.9k 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.
  • T Offline
    T Offline
    takoo
    wrote on last edited by
    #1

    How to add Action specific index on Qtreeview ?

    I just write code ------- (-_-) ----------

    kshegunovK 1 Reply Last reply
    0
    • T takoo

      How to add Action specific index on Qtreeview ?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @takoo
      Hello,
      I don't understand the question, you have to elaborate. What is it you're trying to achieve?

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        takoo
        wrote on last edited by takoo
        #3

        Hi @kshegunov
        example :
        Apple
        ---a
        ---b

        Pear
        ---c
        ---d

        How to make another context menu for Pear and How to make another context menu for Apple?

        so Apple and Pear context menu will be different.

        I just write code ------- (-_-) ----------

        kshegunovK 1 Reply Last reply
        0
        • T takoo

          Hi @kshegunov
          example :
          Apple
          ---a
          ---b

          Pear
          ---c
          ---d

          How to make another context menu for Pear and How to make another context menu for Apple?

          so Apple and Pear context menu will be different.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @takoo
          Okay. You can intercept the context menu events for the tree view. Are you working with a QTreeView subclass? Let's assume you are; you can override the QWidget::contextMenuEvent handler and process the event yourself. Then from the QContextMenuEvent you can retrieve the position in widget coordinates, and finally you can get the QModelIndex that's corresponding to the item at that poistion through the QAbstractItemView::indexAt method. Here's a bit more information on the subject (it's an example from the docs). Something like this should suffice to get you started:

          class MyTreeView : public QTreeView
          {
              Q_OBJECT
          public:
              // ...
          protected:
              virtual void contextMenuEvent(QContextMenuEvent *) Q_DECL_OVERRIDE;
          };
          
          void MyTreeView::contextMenuEvent(QContextMenuEvent * event)
          {
              QPoint position = event->pos();
              QModelIndex itemIndex = indexAt(position);
          
              // ... Decide based on the model index what context menu you'll be showing ...
          }
          

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2
          • T Offline
            T Offline
            takoo
            wrote on last edited by
            #5

            Thanks for reply

            I just write code ------- (-_-) ----------

            1 Reply Last reply
            0
            • T Offline
              T Offline
              takoo
              wrote on last edited by
              #6

              @kshegunov I did not understand anything. Please Can you give me example code (.cpp including)

              I just write code ------- (-_-) ----------

              kshegunovK 1 Reply Last reply
              0
              • T takoo

                @kshegunov I did not understand anything. Please Can you give me example code (.cpp including)

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #7

                @takoo said:

                Please Can you give me example code (.cpp including)

                But I did, I sourced a full example from the Qt documentation with 4 files (at the bottom), the header, the sources and the project file.
                Did you miss the link? Here it is:
                http://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html

                Kind regards.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0

                • Login

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