Results 1 to 4 of 4

Thread: Autoexclusive property for QPushButtons

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

    Default Autoexclusive property for QPushButtons

    So I have a bunch of QPushButtons in QGridLayout and they all have autoExclusive = true. These buttons are loaded from a .ui file.
    I added two new QPushButtons to the layout programmatically, and set the same property autoExclusive to true for them.

    In my code when each button is clicked I have the following

    Qt Code:
    1. if (!button->isChecked())
    2. {
    3. button->setChecked(true);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Now when I click on either of these two new QPushButtons, they don't get unchecked when I click any other button, but the rest of them do, so I'm not sure why the autoexclusive property doesn't work for these buttons that were added programmatically.

  2. #2
    Join Date
    Jul 2017
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Autoexclusive property for QPushButtons

    I haven't been able to find a cleaner solution with respect to the auto exclusive property, but the following works for me in the meantime:

    Qt Code:
    1. QGridLayout* gL = QWidget::findChild<QGridLayout *>("buttonsLayout");
    2. QPushButton* btn = 0;
    3. for(int i = 0; i < gL->count(); ++i)
    4. {
    5. if(btn = dynamic_cast<QPushButton*>(gL->itemAt(i)->widget()))
    6. {
    7. if(btn != button) // where button is the button that was just recently clicked.
    8. {
    9. btn->setChecked(false);
    10. }
    11.  
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: Autoexclusive property for QPushButtons

    Try using a QButtonGroup to hold both your static and dynamic buttons. You can set the exclusive property of the button group, which will enforce that for all of the checkable buttons in it. QButtonGroup does not have any visual appearance, it is just a container for holding buttons and enforcing policies on them as a group.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    qtstarter121 (18th April 2018)

  5. #4
    Join Date
    Jul 2017
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Autoexclusive property for QPushButtons

    Thanks for your reply. It makes sense to me that using a QButtonGroup would work well.

Similar Threads

  1. Replies: 3
    Last Post: 2nd April 2016, 12:46
  2. Replies: 1
    Last Post: 24th November 2014, 09:28
  3. Best widget to use as container for row of QPushButtons?
    By Coolname007 in forum Qt Programming
    Replies: 3
    Last Post: 25th November 2012, 21:24
  4. Where is my QPushButtons
    By HelloDan in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2009, 08:15
  5. Mouse Over Event on QPushButtons
    By merry in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2007, 14:23

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.