tags:

views:

276

answers:

2

I need to display a context menu whenever a tab is clicked on and it needs to react to that specific tab. Is there any way to do this without subclassing it?

A: 

I think you need to create your own class that inherits from QTabWidget and override the MousePressEvent(QMouseEvent) protected function in which you can create your context menu on right click.

Patrice Bernassola
-1 since he specifically asked for a way to achieve what he wants without subclassing QTabWidget.
Frerich Raabe
If he is not familiar with the installEventFilter, the inheritance is an easier way. But let him try your second solution first.
Patrice Bernassola
+2  A: 

Easy way, but possibly not precisely what you ened:

  1. Connect to the 'currentChanged' signal of your QTabWidget
  2. In the slot which is connected to the signal, create a QMenu and populate it as needed
  3. Finally, in the slot which is connected to the signal, call QMenu::exec( QCursor::pos() )

This will get a function called whenever the tab is changed (not necessarily clicked) and spawn a menu at the current mouse position.

Complicated way, which exactly does what you describe:

  1. Call QObject::installEventFilter on your QTabWidget, so that all the events on your QTabWidget are redirected to your own object.
  2. In your own object, reimplement QObject::customEvent and handle all QMouseEvent events.
  3. Populate a QMenu as needed and call QMenu::exec at the position of the QMouseEvent you're handling.
Frerich Raabe
Your first easy solution does not works as he wants. Context menu will not be display is mouse click is done on current tab.The only solution seems to be the your second one.
Patrice Bernassola