7

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?

ymoreau
  • 3,402
  • 1
  • 22
  • 60
Donotalo
  • 12,748
  • 25
  • 83
  • 121

1 Answers1

24

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.

feedc0de
  • 3,646
  • 8
  • 30
  • 55
Simon Broadhead
  • 3,483
  • 21
  • 19
  • 2
    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) – David Souther Mar 04 '10 at 00:11
  • 5
    Or just use menu.exec(QCursor::pos()) http://doc.trolltech.com/latest/qmenu.html#exec – Amree Aug 10 '10 at 05:36
  • Context menu may be called from the keyboard shortcut, so cursor position is not always the correct point of call. – trig-ger Dec 07 '17 at 09:11