Results 1 to 5 of 5

Thread: Using QButtonGroup in Designer - Ways to set ID?

  1. #1
    Join Date
    Feb 2011
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Using QButtonGroup in Designer - Ways to set ID?

    Hi,

    I have done some searching around on the forums about how to use QButtonGroup and found many of the posts are old, and took a bit to translate in to the latest version. I have things working partially now, but I can't figure out if there is a way to make it assign the ID in a reasonable manner.

    Here is my setup. I have 20 buttons in a 4 by 5 grid. This was created using Designer in Qt Creator.

    Note: to create the QButtonGroup by right clicking only works when you right click in the Object Inspector, and is not an option if right clicking on the selected buttons themselves. Other forum posts did not say this so it took a while for me to figure that out.

    I placed a connect statement in the constructor and was able to get the index output as a qDebug in the destination function. This is where the weirdness starts. I am aware that the auto index uses negative numbers and that is not a problem, but it seems to start from right to left instead of left to right. My index output looks like this. (organised in the arrangement of the buttons)

    Qt Code:
    1. -5 -4 -3 -2
    2. -9 -8 -7 -6
    3. -13 -12 -11 -10
    4. -17 -16 -15 -14
    5. -21 -20 -19 -18
    To copy to clipboard, switch view to plain text mode 

    How can I change this behavior in Designer? The default tab order had the same numbers, but after changing that in Designer it didn't effect the numbering order. I have multiple sets of these button grids so I am trying to avoid having to call setId for each button separately.

    I want to trigger a process_buttons(int index) function with the index going left to right, top to bottom. My process_buttons(int) function needs the index in the following mapping. I'm not sure if I need zero indexing, but subtracting a constant isn't a big issue.

    Qt Code:
    1. 1 2 3 4
    2. 6 7 8 9
    3. 10 11 12 13
    4. 14 15 16 17
    5. 18 19 20 21
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using QButtonGroup in Designer - Ways to set ID?

    Note: to create the QButtonGroup by right clicking only works when you right click in the Object Inspector, and is not an option if right clicking on the selected buttons themselves. Other forum posts did not say this so it took a while for me to figure that out.
    Right-clicking on a single button does not allow you to create a button group, but if one already exists you get group related options. Right-clicking with multiple selected buttons will allow you to create a new button group.

    AFAICT there's no way to explicitly set the id of a widget in the button group in Designer. Automatic IDs are dependent on the order of their adding to the generated QButtonGroup, which with uic generated code appears to be the order of addition of the button to the Designer form. I've not seen that documented so I would not rely on it.

    In your form constructor I would simply call QButtonGroup::setId() for each button and set the ID you want. If you adopt consistent button naming then you might be able to reduce this to a loop over the buttons in the group, extracting the id from the name for example.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QButtonGroup in Designer - Ways to set ID?

    Since your grid seems to define the button ids, you could iterate over it line by line, each line in the direction you want and just count the ID value up as you go.

    Cheers,
    _

  4. #4
    Join Date
    Feb 2011
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QButtonGroup in Designer - Ways to set ID?

    I am not referring to right clicking a single button. When I select multiple buttons in Designer and right click on the selected buttons it seems to interpret that as a right click on a single button instead of the selection. Anyway that is just a side annoyance.

    My buttons are named in the following manner.

    group_aux1
    aux1_Button1
    aux1_Button2
    etc...

    Any tips on how to do a for loop over these so I can call setID() for each without tons of lines of code? I haven't done that type of thing before so please excuse my ignorance. I would appreciate at least a some links to the appropriate docs so I can learn faster.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using QButtonGroup in Designer - Ways to set ID?

    Each button is a QObject and has an objectName(), and Designer sets the objectName() to match the name you see on the Object Inspector panel. You can iterate over QPushButton children of the form (or some child widget containing the buttons) where the name matches a pattern, extract the numeric part and use that as the ID. Something like:
    Qt Code:
    1. ui->setupUi(this);
    2.  
    3. const QRegExp re("aux1_button(\\d+)");
    4. foreach(QPushButton *button, findChildren<QPushButton*>(re)) {
    5. (void) re.indexIn(button->objectName()); // we know it matches, but we need the captured text
    6. const int id = re.cap(1).toInt();
    7. ui->buttonGroup->setId(button, id); // assuming the button is already in the button group
    8. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Is there any ways to check for nan inf and -inf in Qt?
    By aarelovich in forum Qt Programming
    Replies: 5
    Last Post: 2nd June 2016, 13:29
  2. Ways to share data between threads
    By AndresBarbaRoja in forum Qt Programming
    Replies: 8
    Last Post: 11th March 2011, 17:47
  3. Replies: 2
    Last Post: 16th July 2010, 16:03
  4. Two ways, Which one is better?
    By HelloDan in forum Qt Programming
    Replies: 19
    Last Post: 30th March 2009, 23:24
  5. ways to exit appl.
    By whoops.slo in forum Newbie
    Replies: 1
    Last Post: 29th January 2007, 19:18

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.