[Qt-interest] Adding menu for tabwidget

Andre Somers andre at familiesomers.nl
Thu Aug 20 10:56:39 CEST 2009


Yuvaraj R wrote:
> Dear All
>  
>  
> I have near 10 tabs in my tab widget.. I don't like to display the 10 
> tabs in tabwidget..
>  
> I want to provide menu for tabwidget..
>  
> whenever user clciking the first tab ,menu will be display.
>  
> Any ideas?
>  
> how do we get the notification,suppose  first tab icon is pressed..
>  
>  
Not that hard, I guess. I did not try it myself, but I would go about it 
like this:

QTabWidget is internally using QTabBar.
Subclass your QTabWidget, and re-implement virtual bool 
eventFilter(QObject*, QEvent*). Now,
install an eventfilter on the internal tabBar from your subclassed 
QTabWidget's constructor by calling tabBar()->installEventFilter(this). 
Now, your eventFilter method will get all the events mend for the tabBar.
In in the event filter, you listen for events mend from your tabBar. You 
will probably be interested in events of type QEvent::MouseButtonPress. 
If you find such an event, you can use QTabBar::tabAt() to find out on 
which tab the user clicked. Return true from your eventFilter 
implementation if you handled the event, and false if you don't (that 
will be the majority of events!). If you forget this, your tabs will not 
do anything anymore.

To pop up a menu, simply populate a QMenu object and call it's popup 
method. You can use the coordinates of the mouse press event to position 
your popup menu.

Something along those lines should work, I think.

André




More information about the Qt-interest-old mailing list