Context Menus



  • I currently have a tab bar that is the central widget for my main window. The user can create several tabs by inputting appropriate information, and each tab is a widget of it's own. If I wanted to implement a context menu for each individual tab, would I need to create a new class for that tab subclassed from QTabWidget?

    I'm asking this because my Tab widget is currently declared in my main window class, and it seems it would be more complicated to use the main window context menu event handler class to determine which tab triggered the context menu. I get the impression that in order to have a context menu for a widget, I must either pass the event on to the parent or implement a context menu event handler for the widget (which would mean I'd have to subclass the tab widget).

    Is this correct?



  • You can set the "Context menu policy":http://doc.trolltech.com/latest/qwidget.html#contextMenuPolicy-prop on a widget. If you set it to customContextMenu, the signal customContextMenuRequested() is emitted. You could then connect to that signal, and use the sender() information to determine which page triggered the context menu.



  • @
    void VCMainWindow::CreateNewDataTab( )

    { /BEGIN FUNCTION CREATENEWDATATAB()/

    VCWIDGETPTR NewDataTab       = NULL;
    
    NewDataTab = new VCWIDGET;           //Creates an instance of a new data tab
    
    NewDataTab->setContextMenuPolicy( Qt::CustomContextMenu );
    
    connect( NewDataTab, SIGNAL( customContextMenuRequested( ) ), this, SLOT( ShowTabContextMenu(  ) ) );
    
    VCTabBar->addTab( new VCMPDataTabLayout( this ), "New Tab");
    

    } /CLOSE FUNCTION CREATENEWDATATAB()/@

    I tried that here...it keeps telling me:

    Object::connect: No such signal QWidget::customContextMenuRequested( ) in ..\VCCalculator\source_files\vcwindows.cpp:202



  • If you look at the docs, the signal has this signature:

    @
    void customContextMenuRequested ( const QPoint & pos );
    @

    if you use the correct signatrure, it would work:

    @
    void VCMainWindow::CreateNewDataTab( )

    { /BEGIN FUNCTION CREATENEWDATATAB()/

    VCWIDGETPTR NewDataTab       = NULL;
    
    NewDataTab = new VCWIDGET;           //Creates an instance of a new data tab
    
    NewDataTab->setContextMenuPolicy( Qt::CustomContextMenu );
    
    connect( NewDataTab, SIGNAL( customContextMenuRequested( const QPoint & ) ), this, SLOT( ShowTabContextMenu(  ) ) );
    
    VCTabBar->addTab( new VCMPDataTabLayout( this ), "New Tab");
    

    } /CLOSE FUNCTION CREATENEWDATATAB()/
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.