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. QTableView context menu get QModelIndex information
Forum Updated to NodeBB v4.3 + New Features

QTableView context menu get QModelIndex information

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 4.4k Views 1 Watching
  • 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #4

    You have the index where the click occurred - what's the problem remembering this until the action gets executed??

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mr Gisa
      wrote on last edited by
      #5

      How am I going to remember? The action and the connection of the triggered signal is not in the same method.
      The context menu is shown in one slot, but it's created in another method with the actions.

      JonBJ VRoninV 2 Replies Last reply
      0
      • M Mr Gisa

        How am I going to remember? The action and the connection of the triggered signal is not in the same method.
        The context menu is shown in one slot, but it's created in another method with the actions.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #6

        @Mr-Gisa
        I don't claim to know/understand just what you're doing here, but in some shape or form you save the desired information in, say, a member variable and access that from the other function. If you're saying your mTableContextMenu->popup(point) needs to know about the index, instead of directly creating a base QMenu you could derive from that to add a member to hold the extra information, and set that from your customContextMenuRequested() before calling popup().

        1 Reply Last reply
        1
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #7
          void MainWindow::customContextMenuRequested(const QPoint &pos){
          const QModelIndex index = ui->resultsTableView->indexAt(pos);
          if (index.isValid()) {
          const QPoint point = ui->tableView->viewport()->mapToGlobal(pos);
          QMenu* tableContextMenu = new QMenu(this);
          connect(tableContextMenu,&QMenu::aboutToHide,tableContextMenu,&QMenu::deleteLater);
          QAction* copyUrlAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-copy"),QIcon(QStringLiteral(":/copy.png")), tr("Copy Url to Clipboard"),tableContextMenu);
          const QString urlString = index.data().toString();
          connect(copyUrlAction,&QAction::triggered,this,[urlString]()->void{
          QMimeData* mimeDt = new QMimeData;
          mimeDt->setUrls({QUrl::fromUserInput(urlString)});
          QGuiApplication::clipboard()->setMimeData(mimeDt);
          });
          tableContextMenu->addAction(copyUrlAction);
          tableContextMenu->popup(point);
          }
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2
          • M Mr Gisa

            How am I going to remember? The action and the connection of the triggered signal is not in the same method.
            The context menu is shown in one slot, but it's created in another method with the actions.

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #8

            @Mr-Gisa said in QTableView context menu get QModelIndex information:

            How am I going to remember? The action and the connection of the triggered signal is not in the same method.
            The context menu is shown in one slot, but it's created in another method with the actions.

            You cold also add a private member in MainWindow and remember it that way

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mr Gisa
              wrote on last edited by
              #9

              I just noticed something, it returns the QModelIndex for the current right clicked item in the table, so when using QModelIndex::data it will return only the data of that item.

              I wanted to create one single context menu with something like:

              Copy Url to Clipboard
              Copy Title to Clipboard

              Url and Title are two different columns. I want that independently of the item I right click it appears the same context menu and copy the right item.

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #10

                You can use index.model()->index(index.row(),URL_COLUMN,index.parent()).data() index.model()->index(index.row(),TITLE_COLUMN,index.parent()).data()

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mr Gisa
                  wrote on last edited by Mr Gisa
                  #11

                  I did differently, as the user can only select one row, I used like this:

                  auto selected = ui->resultsTableView->selectionModel()->selectedIndexes();
                  

                  In the QAction::triggered signal and this way I can do something like selected.at(0) to get the QModelIndex for the first column.

                  Is there something wrong with that approach?

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #12

                    @Mr-Gisa said in QTableView context menu get QModelIndex information:

                    Is there something wrong with that approach?

                    I might be wrong here but I thought that if you right clicked on a item that it is not selected it does not get selected

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mr Gisa
                      wrote on last edited by
                      #13

                      Actually it does, if you right click on an unselected item it does get selected.

                      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