Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How to add Action specific index on Qtreeview ?

    General and Desktop
    2
    7
    1384
    Loading More Posts
    • 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
      takoo last edited by

      How to add Action specific index on Qtreeview ?

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

      kshegunov 1 Reply Last reply Reply Quote 0
      • kshegunov
        kshegunov Moderators @takoo last edited by kshegunov

        @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 Reply Quote 0
        • T
          takoo last edited by 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.

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

          kshegunov 1 Reply Last reply Reply Quote 0
          • kshegunov
            kshegunov Moderators @takoo last edited by

            @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 Reply Quote 2
            • T
              takoo last edited by

              Thanks for reply

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

              1 Reply Last reply Reply Quote 0
              • T
                takoo last edited by

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

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

                kshegunov 1 Reply Last reply Reply Quote 0
                • kshegunov
                  kshegunov Moderators @takoo last edited by kshegunov

                  @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 Reply Quote 0
                  • First post
                    Last post