Results 1 to 6 of 6

Thread: add actiongroup to toolbar

  1. #1
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default add actiongroup to toolbar

    How can I add a QActionGroup to a toolbar with C++ (ie. without using Qt Designer)?
    Last edited by ravas; 22nd July 2015 at 04:12.

  2. #2
    Join Date
    Jul 2015
    Location
    Sweden
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: add actiongroup to toolbar

    I think this (link) is what your looking for. Searching the forums for you there, but I were wondering why it wasn't possible to do it in the designer.

  3. #3
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: add actiongroup to toolbar

    I see code that adds actions to an actiongroup.
    I don't see anything about adding the actiongroup to a toolbar.

    I don't need to do this at all; I'm curious.

  4. #4
    Join Date
    Jul 2015
    Location
    Sweden
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: add actiongroup to toolbar

    But that's because you don't really need any more code. The docs do say you usually should create a QActionGroup and set it as the parent when creating the QActions belonging to it. But I've yet to see any code sample for that. Not that I've been looking for one.
    All you do is add a few actions to a toolbar or menu and then create a group and add the relevant actions to it. Nothing else is needed. The group it self is newer added to anything, but setting the window as an owner will assure proper cleanup.

    Edit: Now I did find a sample looking for something entirely different...

    Qt Code:
    1. actionGroup = new QActionGroup(this);
    2. a1 = actionGroup->addAction("a1");
    3. a2 = actionGroup->addAction("a2");
    4. ...
    5. toolbar->addActions(actionGroup->actions());
    To copy to clipboard, switch view to plain text mode 

    Still doesn't add the action group itself to the toolbar only the actions it owns.
    Last edited by mikag; 22nd July 2015 at 12:07. Reason: updated contents

  5. The following user says thank you to mikag for this useful post:

    ravas (13th November 2015)

  6. #5
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: add actiongroup to toolbar

    The docs do say you usually should create a QActionGroup and set it as the parent when creating the QActions belonging to it. But I've yet to see any code sample for that.
    Simply pass the actiongroup as the parent argument when you create an action.

    Qt Code:
    1. new QAction(tr("&Some Action"), action_group);
    To copy to clipboard, switch view to plain text mode 

    All you do is add a few actions to a toolbar or menu and then create a group and add the relevant actions to it. Nothing else is needed.
    The point is that adding the actiongroup would require less code than adding actions individually.

    Qt Code:
    1. toolbar->addActions(actionGroup->actions());
    To copy to clipboard, switch view to plain text mode 

    That is what I was looking for; thank you.
    Last edited by ravas; 22nd July 2015 at 19:07.

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: add actiongroup to toolbar

    The more useful thing about a QActionGroup is that you can enable / disable the group, which enables / disables all actions in it (and the toolbar / menu items to which the actions are attached). This is more efficient and less error-prone that having to control actions individually based on some state of your program.

  8. The following user says thank you to d_stranz for this useful post:

    ravas (13th November 2015)

Similar Threads

  1. About Qt Toolbar
    By korg1988 in forum Qt Programming
    Replies: 5
    Last Post: 10th February 2013, 19:12
  2. Replies: 5
    Last Post: 11th September 2010, 14:29
  3. Palette toolbar need help plz...
    By Radagast in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2008, 14:08
  4. Toolbar
    By assismvla in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2008, 17:42
  5. right justify toolbar
    By magland in forum Qt Programming
    Replies: 5
    Last Post: 16th April 2008, 18:36

Tags for this Thread

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.