Creating popup menu in Qt for QTableView

14,722

Check out the customContextMenuRequested signal to get the event, and use a QMenu for the menu itself. Use QTableView::indexAt to find out what, if any, cell was clicked based on the coordinates given to the signal and take the appropriate action when a menu item is clicked.

Share:
14,722
Donotalo
Author by

Donotalo

Yet to be discovered.

Updated on June 07, 2022

Comments

  • Donotalo
    Donotalo about 2 years

    I have a QTableView in the main UI of my program. I'd like to show popup menu when user right clicks on the cells of the table and take appropriate action when an option is selected from the menu.

    I am using Qt Creator 1 (Qt version 4.5). How can I do that?

  • David Souther
    David Souther over 14 years
    You will need to call menu.exec(const QPoint&) to display it. Make sure you translate it to the appropriate coordinates: menu.exec(mapToGlobal(point), 0)
  • RushHour
    RushHour almost 14 years
    Or just use menu.exec(QCursor::pos()) doc.trolltech.com/latest/qmenu.html#exec
  • trig-ger
    trig-ger over 6 years
    Context menu may be called from the keyboard shortcut, so cursor position is not always the correct point of call.