+ Reply to Thread
Results 1 to 5 of 5

Thread: How to disable context menu of a toolbar in QMainApplication

  1. #1
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to disable context menu of a toolbar in QMainApplication

    I have a small application that inherits QMainWindow. I have implemented my own mechanism enabling user to show and hide Toolbars. It is placed in QMenuBar and contains a few checkable actions. The problem is that when one clicks a right button on a free space on a ToolBar he gets the context menu that enables him to show and hide toolbars. Thus when one switches one toolbar off in this context menu, the checkable action i menubar remains checked as it is not related to the context menu. I tried to reimplement QMouseEvent and simply do nothing inside just to remove the feature of ToolBar context menu but it didn't work. So here comes my question:

    How to disable context menu that emerges when we click right mouse button on a QToolBar that is a part of QMainApplication class?

    MJ

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 540 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to disable context menu of a toolbar in QMainApplication

    You tried with the wrong event.
    The event is called QContextMenuEvent.

    The best choice is to install the main window as event filter for all the toolbars and simply catch the context menu event( don't let it reach the toolbar(s)).

    If you have problems implementing/understanding this, you can ask here.

    Regards

  3. The following user says thank you to marcel for this useful post:

    mtrpoland (25th June 2007)

  4. #3
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to disable context menu of a toolbar in QMainApplication

    Something like this?

    MWin inherits QMainWindow

    Qt Code:
    1. bool MWin::eventFilter(QObject* object, QEvent* event) {
    2. if(event->type() == QEvent::ContextMenu) {
    3. QContextMenuEvent* mevent = static_cast<QContextMenuEvent *>(event);
    4. if(mevent->reason() == QContextMenuEvent::Mouse) {
    5. qDebug("I have blocked the context menu.");
    6. return true;
    7. } else return QObject::eventFilter(object, event);
    8. }
    9. else return QObject::eventFilter(object, event);
    10. }
    To copy to clipboard, switch view to plain text mode 

    and installation:

    Qt Code:
    1. ...
    2. fileToolBar->installEventFilter(this);
    3. ...
    4. modeToolBar->installEventFilter(this);
    5. ...
    6. actionToolBar->installEventFilter(this);
    7. ...
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 540 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to disable context menu of a toolbar in QMainApplication

    More like this. You must test for the objects you're installing the filter for.
    Qt Code:
    1. bool MWin::eventFilter(QObject* object, QEvent* event) {
    2. if(event->type() == QEvent::ContextMenu &&
    3. ( object == fileToolbar || object == modeToolBar ... ) ) {
    4. QContextMenuEvent* mevent = static_cast<QContextMenuEvent *>(event);
    5. if(mevent->reason() == QContextMenuEvent::Mouse) {
    6. qDebug("I have blocked the context menu.");
    7. return true;
    8. } else
    9. return false;
    10. }
    11. else
    12. return QObject::eventFilter(object, event);
    13. }
    To copy to clipboard, switch view to plain text mode 


    Regards

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,507 Times in 1,384 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to disable context menu of a toolbar in QMainApplication

    The preferred way would be to use QToolBar::toggleViewAction() in the menu bar. If you want to create a custom context menu, reimplement QMainWindow::createPopupMenu(). If you want to disable the context menu, you can set the context menu policy of the main window to Qt::NoContextMenu.
    J-P Nurmi

+ Reply to Thread

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 09:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts