Results 1 to 5 of 5

Thread: Context menu for QAction

  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Context menu for QAction

    I have a main window with a toolbar which I've added some QActions to.
    QToolBar::addAction lets me specify a slot to be called when the action is triggered, i.e. clicked on with the left mouse button.

    How can I have a slot called when right-clicking the QAction (for e.g. a context menu)?

  2. #2
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Context menu for QAction

    If right-clicking is not a strict requirement for you, you can manually add a QToolButton to the toolbar and set a menu with QToolButton::setMenu(). You can find an image of the resulting button at the very bottom of this page:

    http://qt.nokia.com/doc/4.5/qpushbutton.html#setMenu

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

    drhex (29th September 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Context menu for QAction

    It seems I had confused QMenuBar with QToolBar. Let's start over.

    I have a QMainWindow with a QMenuBar on top. There are only a few menus and I'd like to use the remaining space in the menu bar for tool buttons. QMenuBar does not have an addWidget(), and its layout() returns NULL so I can insert a widget through its layout.
    I can insert QActions though, and they can call a slot when clicked.

    The tool button I'm having a problem with has an obvious action associated with it that I want to activate by clicking - no problem. But it also has some oddball-functions that I'd like to access through a menu by right-clicking it (or possibly through the little arrow that QToolButton has).
    I've tried:

    1) Creating a QAction whose triggered() signal activates the main function and the rest through setMenu() on the QAction prior to inserting it into the QMenuBar. This fails - left click opens the menu and right click does nothing.

    2) Inserting a QAction into the QMenuBar that really is a QWidgetAction whose createWidget() returns a QToolButton or some other custom widget where I reimplement mousePressEvent() to handle both mouse buttons. Fails because createWidget() is never called when the QWidgetAction is inserted into the QMenuBar.

    Is it true then that QMenuBar cannot take a QWidget and that it also insists on consuming the entire width of the QMainWindow even if it has only a small number of menus? (otherwise it might be possible to put a QToolBar to the right of the QMenuBar)

  5. #4
    Join Date
    Jun 2009
    Posts
    12

    Default Re: Context menu for QAction

    Im not sure but that would be consistent with every Gui I saw the last 15 years.

  6. #5
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Context menu for QAction

    Well, I have Google's search field immediately to the right of the menubar in Firefox as I type this.

    The solution that seems promising now is to not let the QMainWindow have a menubar, but rather let the central widget of the QMainWindow have a QMenuBar at the top with a QToolbar next to it.

Similar Threads

  1. problem with Context Menu in QTableWidget
    By andreime in forum Newbie
    Replies: 6
    Last Post: 7th September 2015, 08:44
  2. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 09:52
  3. Custom context menu in QTreeView
    By ttvo in forum Qt Programming
    Replies: 5
    Last Post: 3rd April 2009, 22:29
  4. Shortcut key for context menu
    By darshan.hardas in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 20:32
  5. Replies: 4
    Last Post: 25th June 2007, 20:40

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.