Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Unsolved Autoexclusive property for QPushButtons

    General and Desktop
    3
    7
    1306
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Q
      Qtstarter121 last edited by SGaist

      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 = true for them.

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

        if (!button->isChecked())
         {
            button->setChecked(true);
         }
      

      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.

      1 Reply Last reply Reply Quote 0
      • Q
        Qtstarter121 last edited by Qtstarter121

        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:

           QGridLayout* gL = QWidget::findChild<QGridLayout *>("buttonsLayout"); 
           QPushButton* btn = 0;
           for(int i = 0; i < gL->count(); ++i)
           {
             if(btn = dynamic_cast<QPushButton*>(gL->itemAt(i)->widget())) 
             {
                if(btn != button) // where button is the button that was just recently clicked.
                {
                   btn->setChecked(false);
                }
        
             }
           }
        
        
        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Are you looking for QButtonGroup::setExclusive ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply Reply Quote 0
          • Q
            Qtstarter121 @SGaist last edited by

            @SGaist No because my buttons are not part of a QButtonGroup. They are simply arranged in a QGridLayout.

            mrjj 1 Reply Last reply Reply Quote 0
            • mrjj
              mrjj Lifetime Qt Champion @Qtstarter121 last edited by

              @Qtstarter121

              Hi.
              autoExclusive only works if they have same parent.
              So check what the working buttons parent is -
              and set the same for the new dynamically allocated buttons.
              If the first buttons are inserted with Designer,
              you can go and see what is set as parent (in setupUI() )
              and use the same.

              Q 1 Reply Last reply Reply Quote 0
              • Q
                Qtstarter121 @mrjj last edited by

                @mrjj Yes the other buttons were created with Qt Designer in a .ui file. I actually looked at the code for the UI and I ensured that my programmatically created buttons had the same parent, but no change. That is why I'm confused as to why this is happening.

                mrjj 1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion @Qtstarter121 last edited by

                  @Qtstarter121
                  That is very odd as i did that as a small test.
                  Stole the code from setupUI and added via button.clicked() and
                  it the worked as the Designer inserted ones.
                  I just used a widget with a layout.

                  Have you tried a small test ? ( to rule out something else in actual project)

                  1 Reply Last reply Reply Quote 1
                  • First post
                    Last post