Qt World Summit: Submit your Presentation

[SOLVED] ignore QWidget of making QRadioButton exclusive

  • i have two QWidget which every has a QRadioButton contained. i wanna make those widgets not to make the child QRadioButton exclusive and let the parent to host the exclusivity.
    using layouts wouldn't be an option. any solution?


  • You could try to use a [[doc:QButtonGroup]] explicitly. Create an instance, and add your radio buttons to the group. Then set the group to be exclusive.

    Note that UI-wise, your design is not recommended. It is very unclear for a user to have radio buttons work as a group when don't visually form a group. So while technically possible, I recommend against doing it. Use radio buttons as a single, vertical list with as little spacing in between as possible. Adding a single dependent checkbox to an item is for instance acceptable, but not much more than that.

  • thanks Andre, it seems i couldn't explain the problem well.
    the previous picture was only a vision for my problem. look, this is our real design


    there's a group box, two widgets which they hold a layout that contains few objects horizontally. unfortunately it's not possible to use only QHBoxLayout inside the groupbox as the objects (inside red rect) would be moved elsewhere outside the QGroupBox. there's other containers which they don't accept layouts.

  • Ok, I guess this layout could work (once you fix the alignment on the right hand side, anyway).

    So, does the QButtonGroup work? Note that a QButtonGroup is not the same as a QGroupBox...

  • look at "Item" and "Service". they are two separated Widgets. they're not only a QRadioButton that could be added into QButtonGroup. the problem is that they are Widgets. widget makes the radio button exclusive.

  • for a better understand
    QWidget *w_first=new QWidget();
    QRadioButton *rb_first= new QRadioButton(w_first);

    QWidget *w_second=new QWidget();
    QRadioButton *rb_second= new QRadioButton(w_second);


    w_first and w_second added into group box

  • Sorry, but you completely lost me. I understand that the radio buttons have different parent widgets. So what?

    Why do you insist on trying to make the whole widget exclusive, instead of just the radio buttons? That doesn't make any sense. Did you actually look at the documentation of QButtonGroup?

    Note that you can achieve the layout that you have with a simple Grid layout too. It would make it easier to keep the radio buttons and lock icons vertically aligned as well.

  • using QButtonGroup or QGroupBox or anything else has no difference. i have other custom container which only accepts Widgets. in the above code w_first and w_second might be placed into that custom container, not just a QGroupBox or QButtonGroup. my objects already laid into a QHBoxLayout but the custom container doesn't accept layouts.

    more complete code
    QWidget *w=new QWidget();
    QHBoxLayout *hbl=new QHBoxLayout(w);

    QLabel *lbl=new QLabel(w);
    QRadioButton *rb= new QRadioButton(w);
    QPushButton *btn=new QPushButton(w);


    say i have several widgets like above. now i want those QRadioButton(s) to be in relation with each other.

    if i could add a layout into the CustomContainer there wouldn't be a problem at all. the problem is those Widgets.

    Moderator edit: Please don't reply to yourself this quickly. Simply edit your post and add to it instead; Andre

  • You completely seem to be missing the point that QButtonGroup is not a widget or a layout. It is a QObject that simply manages the checked state of the buttons (and that includes radio buttons) you add to it. It is completely independent from how or where you put the widgets. For all it cares, the buttons are on different forms completely, and it will still manage the checkstates. So all your text about how the buttons are in widgets or layouts or that a container won't accept layouts is completely irrelevant.

    Again: did you actually read the documentation of QButtonGroup? Or did you stop reading after the first sentence or two?

    It really is as simple as adding:
    QButtonGroup* group = new QButtonGroup(this);
    // etc.

    to the code above.

  • Andre, i already know about QButtonGroup :) but please note i'm not talking about basics or a usual action.
    so all it seems, there's not possible to set the QWidget not act like an exclusive container. i should add another condition to check if the container is the "Custom" one, so it should add items in another way; else i'll add only the layout into my container (might be a QGroupBox or anything).
    BTW, thanks for your time.

  • oh wait... i'm now understanding what you say!!! i was misunderstood your expression. wait a minute i'll check your solution. it seems to be what i expect.

  • Andre, you were right. i was completely missed the concept of QButtonGroup due to the high working pressure these days.
    Thanks a million time.

  • [quote author="Mohsen" date="1349088340"]Andre, you were right. i was completely missed the concept of QButtonGroup due to the high working pressure these days.
    Thanks a million time.[/quote]

    Ok, so you did not read and understand the docs. Note that it would have saved you (and me) time if you'd just done that when you were told that the first time around...

    Anyway, glad you got it working. -Please mark the topic as [Solved].- (You already did that).

Log in to reply