Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Autoexclusive property for QPushButtons
Forum Updated to NodeBB v4.3 + New Features

Autoexclusive property for QPushButtons

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 3.2k Views 3 Watching
  • 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 Offline
    Q Offline
    Qtstarter121
    wrote on last edited by SGaist
    #1

    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
    0
    • Q Offline
      Q Offline
      Qtstarter121
      wrote on last edited by Qtstarter121
      #2

      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
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        0
        • SGaistS SGaist

          Hi,

          Are you looking for QButtonGroup::setExclusive ?

          Q Offline
          Q Offline
          Qtstarter121
          wrote on last edited by
          #4

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

          mrjjM 1 Reply Last reply
          0
          • Q Qtstarter121

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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
            0
            • mrjjM mrjj

              @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 Offline
              Q Offline
              Qtstarter121
              wrote on last edited by
              #6

              @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.

              mrjjM 1 Reply Last reply
              0
              • Q Qtstarter121

                @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.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved