3

I am using a QTableWidget for displaying data. I know that I can use the addAction method to add a context menu. How can I limit the context menu to only specific cells or columns? addAction exists for QActionGroup QGraphicsWidget QMenu QMenuBar QToolBar QWidget. Should I somehow filter oder disable/enable the signal/slots? Work with right click events?

A similar question would be how to get different context menu's for different rows?

Thank you and cheers, Matthias

matthias
  • 247
  • 6
  • 17
  • 1
    You are strongly recommended to use the MVC method for tables. It will also allow you to customize context menus elegantly. – sep May 23 '11 at 10:51

1 Answers1

4

Another method to create custom context menus is to implement a slot to the QWidget::customContextMenuRequested() signal. There you can query the cell under the position (QTableWidget::itemAt() - watch out for global->widget mapping!), and then build a custom menu using QMenu and QAction.

Also, I'd build the menu(s) beforehand, and than only exec() it in the slot.

Remember that you have to change the QWidget::ContextMenuPolicy property of the widget to Qt::CustomContextMenu!

user3797758
  • 829
  • 1
  • 14
  • 28
azyoot
  • 1,162
  • 8
  • 18
  • 1
    Thank you for your answer. That sound's good. Unfortunately I could not bring it up to work. `TableView->setContextMenuPolicy(Qt::CustomContextMenu);` and `connect( TableView, SIGNAL( customContextMenuRequested() ), this, SLOT( on_TableView_ContextMenuRequest() ) );` were added; and of course the slot declared and implemented. Break point is not jumped at and message box for testing does not appear. – matthias May 24 '11 at 14:03
  • Ok, it is `(const QPoint &)`, so `connect( TableView, SIGNAL( customContextMenuRequested(const QPoint &) ), this, SLOT( on_TableView_ContextMenuRequest() ) );` would be right. Thank you! – matthias May 25 '11 at 10:24
  • Okay, let's say this is finally right and makes more sense: `connect( TableView, SIGNAL( customContextMenuRequested(const QPoint &) ), this, SLOT( on_TableView_ContextMenuRequest(const QPoint &) ) );` – matthias May 25 '11 at 10:29