PDA

View Full Version : QTableView contextMenuEvent



derrickbj
1st March 2007, 16:33
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():



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());
}


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);


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:



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());
}


Or does anybody have any other idea on how to do this?

Thanks!

derrickbj
1st March 2007, 17:37
Never mind, I think I've found the answer! :o



void MainWindow::contextMenuEvent(QContectMenuEvent *event)
{
...
QString type = model->data(fileTableView->currentIndex(), Qt:: DecorationRole).typeName();
if (type == "QIcon")
menu.addAction(expandInfoAct);

...

}


Thanks anyway :D