Results 1 to 3 of 3

Thread: radiobutton behavior on menubar

  1. #1
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default radiobutton behavior on menubar

    I'd like to have a menubar with items that are checkable and mutually exclusive of one another. That is I want a series of checkboxes to behave like a radio button group under a menuitem on the menubar. How does one go about that? I've managed to get myself into an infinite loop of signalling when trying to link a toggled( bool ) signal to the toggle() slot of another action and vice versa.

  2. #2
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: radiobutton behavior on menubar

    Quite simple actually.

    Create actions you want on the menu, and add them in a QActionGroup.

    Qt Code:
    1. QMenu *mymeny = new QMenu("mymenu");
    2. QAction *item1 = new QAction("item1");
    3. QAction *item2 = new QAction("item2");
    4. QAction *item3 = new QAction("item3");
    5. mymeny->addAction(item1);
    6. mymeny->addAction(item2);
    7. mymeny->addAction(item3);
    8.  
    9. QActionGroup myGroup = new QActionGroup();
    10. myGroup->addAction(item1);
    11. myGroup->addAction(item2);
    12. myGroup->addAction(item3);
    13.  
    14. connect(item1, SIGNAL(triggered()), someobject, (SLOT(item1slot()));
    15. connect(item2, SIGNAL(triggered()), someobject, (SLOT(item2slot()));
    16. connect(item3, SIGNAL(triggered()), someobject, (SLOT(item3slot()));
    To copy to clipboard, switch view to plain text mode 

    Remember to add either the actions to a menubar directly, or add the menu to a menubar, depending on the layout you want.

    cheers,
    Leif

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

    sp4rk3r (15th July 2008)

  4. #3
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: radiobutton behavior on menubar

    Luf,

    Thank you. I was looking high and low for that. I'm a firm believer in RTFM... and I've got quite a few books regarding QT4, but couldn't find that example anywhere. Google is dead to me.

    My next question is whether or not this approach is easily attainable inside Designer? I don't care if it is, but was curious if the relationship can be established at the layout stage.

    -steve
    Last edited by sp4rk3r; 15th July 2008 at 04: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.