QTableView context menu get QModelIndex information
-
wrote on 30 May 2018, 18:41 last edited by Mr Gisa
I have a context menu like this:
mTableContextMenu = new QMenu(this); mTableContextMenu->addAction(QIcon(":/copy.png"), "Copy Url to Clipboard");
and connected like this:
connect(ui->tableView, &QTableView::customContextMenuRequested, this, &MainWindow::customContextMenuRequested);
Inside the
customContextMenuRequested
:auto index = ui->resultsTableView->indexAt(pos); if (index.isValid()) { auto point = ui->tableView->viewport()->mapToGlobal(pos); mTableContextMenu->popup(point); }
Now I was wondering, how can I get the
QModelIndex
information for theurl
in order to make the action to work? -
How about looking at the documentation: http://doc.qt.io/qt-5/qtableview.html#indexAt ?
-
wrote on 30 May 2018, 19:30 last edited by Mr Gisa
Now I was wondering, how can I get the QModelIndex information for the url in order to make the action to work?
I know that
indexAt
will give me theQModelIndex
but as I said, I need to make the action to work with the current right clicked item, like if I presscopy url to clipboard
I need theQModelIndex
in order to get the value but I don't have that in the slot that I connected to the actiontriggered
signal. -
You have the index where the click occurred - what's the problem remembering this until the action gets executed??
-
wrote on 30 May 2018, 20:02 last edited by
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. -
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.wrote on 31 May 2018, 07:54 last edited by JonB@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 yourmTableContextMenu->popup(point)
needs to know about theindex
, instead of directly creating a baseQMenu
you could derive from that to add a member to hold the extra information, and set that from yourcustomContextMenuRequested()
before callingpopup()
. -
wrote on 31 May 2018, 09:00 last edited by VRonin
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); } }
-
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.wrote on 31 May 2018, 09:04 last edited by@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 -
wrote on 2 Jun 2018, 19:20 last edited by
I just noticed something, it returns the
QModelIndex
for the current right clicked item in the table, so when usingQModelIndex::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 ClipboardUrl
andTitle
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. -
wrote on 2 Jun 2018, 20:28 last edited by
You can use
index.model()->index(index.row(),URL_COLUMN,index.parent()).data()
index.model()->index(index.row(),TITLE_COLUMN,index.parent()).data()
-
wrote on 2 Jun 2018, 20:31 last edited by Mr Gisa 6 Feb 2018, 20:32
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 likeselected.at(0)
to get theQModelIndex
for the first column.Is there something wrong with that approach?
-
wrote on 2 Jun 2018, 20:34 last edited by
@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
-
wrote on 2 Jun 2018, 20:37 last edited by
Actually it does, if you right click on an unselected item it does get selected.
7/13