Results 1 to 2 of 2

Thread: QTableView contextMenuEvent

  1. #1
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QTableView contextMenuEvent

    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():

    Qt Code:
    1. void MainWindow::contextMenuEvent(QContextMenuEvent *event)
    2. {
    3. QMenu menu(this);
    4. menu.addAction(fileOpenAct);
    5. menu.addAction(fileCloseAct);
    6.  
    7. if (model->data(fileTableView->currentIndex()).toString().contains("(OI)")) {
    8. menu.addSeparator();
    9. menu.addAction(expandInfoAct);
    10. }
    11. menu.exec(event->globalPos());
    12. }
    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:

    Qt Code:
    1. model->setData(model->index(row, col ,QModelIndex()), data);
    2. 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:

    Qt Code:
    1. void MainWindow::contextMenuEvent(QContextMenuEvent *event)
    2. {
    3. QMenu menu(this);
    4. menu.addAction(fileOpenAct);
    5. menu.addAction(fileCloseAct);
    6. QModelIndexList list;
    7. list << fileTableView->currentIndex();
    8. if (model->mimeData(list)->hasImage()) {
    9. menu.addSeparator();
    10. menu.addAction(expandInfoAct);
    11. }
    12. menu.exec(event->globalPos());
    13. }
    To copy to clipboard, switch view to plain text mode 

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

    Thanks!

  2. #2
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTableView contextMenuEvent

    Never mind, I think I've found the answer!

    Qt Code:
    1. void MainWindow::contextMenuEvent(QContectMenuEvent *event)
    2. {
    3. ...
    4. QString type = model->data(fileTableView->currentIndex(), Qt:: DecorationRole).typeName();
    5. if (type == "QIcon")
    6. menu.addAction(expandInfoAct);
    7.  
    8. ...
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    Thanks anyway
    Last edited by derrickbj; 1st March 2007 at 16:39. Reason: Correction

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  3. QTableView currentChanged <> selecting header
    By Everall in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2009, 08:24
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.