I wasn't sure how else to title this question...
I have a TableView that gets populated with data from a file. Some cells, depending on the data, will have references to other information from the file, but not populated in the table. I flag these cells, so the user will know there's other information pertaining to this data. My goal is to have the user right-click on these flagged cells and have the contextMenuEvent() add a special QAction for these cells only that allows the user to expand on the information (right now I have the returned added information pop up in a QMessageBox). I began by having the program prepend the flagged data with "(OI) - " and then putting the following in MainWindow::contextMenuEvent():
{
menu.addAction(fileOpenAct);
menu.addAction(fileCloseAct);
if (model->data(fileTableView->currentIndex()).toString().contains("(OI)")) {
menu.addSeparator();
menu.addAction(expandInfoAct);
}
menu.exec(event->globalPos());
}
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(fileOpenAct);
menu.addAction(fileCloseAct);
if (model->data(fileTableView->currentIndex()).toString().contains("(OI)")) {
menu.addSeparator();
menu.addAction(expandInfoAct);
}
menu.exec(event->globalPos());
}
To copy to clipboard, switch view to plain text mode
So when a user right-clicks on a cell not containing flagged data, they only see "Open" and "Close" as options, but when they do click on a cell with flagged data, they see "Open", "Close" and "Expand..."
expandInfoAct would call a slot that would take the information in the cell and pop up a QMessageBox() with the expanded data.
This works, but I wanted to do away with the "(OI) - " and put an icon in the cell, such as a flag, or an arrow or something.
So as the table is being populated, and the program comes across data that has reference to other information (where it would normally prepend the "(OI) - ") it writes the data to the table along with an icon as follows:
model
->setData
(model
->index
(row, col ,
QModelIndex()), data
);
model
->setData
(model
->index
(row, col,
QModelIndex()),
QIcon(":MainWindow/images/flag.png"),Qt
::DecorationRole);
model->setData(model->index(row, col ,QModelIndex()), data);
model->setData(model->index(row, col, QModelIndex()), QIcon(":MainWindow/images/flag.png"),Qt::DecorationRole);
To copy to clipboard, switch view to plain text mode
This brings me to my question. Is there a way for contextMenuEvent() to recognize the flag icon in the cell (as it did with "(OI) - ") when the user right-clicks on the cell? I tried the following and it doesn't work:
{
menu.addAction(fileOpenAct);
menu.addAction(fileCloseAct);
QModelIndexList list;
list << fileTableView->currentIndex();
if (model->mimeData(list)->hasImage()) {
menu.addSeparator();
menu.addAction(expandInfoAct);
}
menu.exec(event->globalPos());
}
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(fileOpenAct);
menu.addAction(fileCloseAct);
QModelIndexList list;
list << fileTableView->currentIndex();
if (model->mimeData(list)->hasImage()) {
menu.addSeparator();
menu.addAction(expandInfoAct);
}
menu.exec(event->globalPos());
}
To copy to clipboard, switch view to plain text mode
Or does anybody have any other idea on how to do this?
Thanks!
Bookmarks