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. PyQT how to get all of the selected checkboxes

PyQT how to get all of the selected checkboxes

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 3.4k Views
  • 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.
  • P Offline
    P Offline
    poordev123
    wrote on last edited by
    #1

    I have an application with 16 checkboxes on it and based on which ones are checked I will set certain values with that. My problem is that I don't want to have to say

    if self.ui.checkbox_1.isChecked():
        ....
    elif self.ui.checkbox2.isChecked():
        ....
    else:
        ....
    

    for all 16 of the checkboxes. Is there a way to just say something along the lines of

    checkbox_list = []
    for checkbox in checkbox_container:
        if checkbox.isChecked():
            checkbox_list.append(checkbox)
    

    or is there a built in way to do this?

    JonBJ jeremy_kJ 2 Replies Last reply
    0
    • P poordev123

      I have an application with 16 checkboxes on it and based on which ones are checked I will set certain values with that. My problem is that I don't want to have to say

      if self.ui.checkbox_1.isChecked():
          ....
      elif self.ui.checkbox2.isChecked():
          ....
      else:
          ....
      

      for all 16 of the checkboxes. Is there a way to just say something along the lines of

      checkbox_list = []
      for checkbox in checkbox_container:
          if checkbox.isChecked():
              checkbox_list.append(checkbox)
      

      or is there a built in way to do this?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @poordev123
      You have to set this up yourself. One way is just to go:

      self.checkbox_list = [ self.ui.checkbox_1, self.ui.checkbox2, ...   ]
      

      once and then re-use that. That's laziest/simplest.

      Another way is to use

      checkbox_list = whatever_parent_they_all_have.findChildren<QCheckBox *>();
      

      That's C++ syntax, it's something very similar in Python. The function is QObject.findChildren() to look up, I think it might be just widget.findChildren(QCheckBox). That does a recursive descent to find all widgets of desired type.

      P 1 Reply Last reply
      2
      • JonBJ JonB

        @poordev123
        You have to set this up yourself. One way is just to go:

        self.checkbox_list = [ self.ui.checkbox_1, self.ui.checkbox2, ...   ]
        

        once and then re-use that. That's laziest/simplest.

        Another way is to use

        checkbox_list = whatever_parent_they_all_have.findChildren<QCheckBox *>();
        

        That's C++ syntax, it's something very similar in Python. The function is QObject.findChildren() to look up, I think it might be just widget.findChildren(QCheckBox). That does a recursive descent to find all widgets of desired type.

        P Offline
        P Offline
        poordev123
        wrote on last edited by
        #3

        @JonB Thanks for your answer, I appreciate it. One question I have though, by parent do you mean the groupbox that they are in? Or the actual parent object of the checkbox class from qt?

        JonBJ 1 Reply Last reply
        0
        • P poordev123

          I have an application with 16 checkboxes on it and based on which ones are checked I will set certain values with that. My problem is that I don't want to have to say

          if self.ui.checkbox_1.isChecked():
              ....
          elif self.ui.checkbox2.isChecked():
              ....
          else:
              ....
          

          for all 16 of the checkboxes. Is there a way to just say something along the lines of

          checkbox_list = []
          for checkbox in checkbox_container:
              if checkbox.isChecked():
                  checkbox_list.append(checkbox)
          

          or is there a built in way to do this?

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          @poordev123 said in PyQT how to get all of the selected checkboxes:

          I have an application with 16 checkboxes on it and based on which ones are checked I will set certain values with that. My problem is that I don't want to have to say

          if self.ui.checkbox_1.isChecked():
              ....
          elif self.ui.checkbox2.isChecked():
              ....
          else:
              ....
          

          for all 16 of the checkboxes. Is there a way to just say something along the lines of

          checkbox_list = []
          for checkbox in checkbox_container:
              if checkbox.isChecked():
                  checkbox_list.append(checkbox)
          

          These two snippets don't do the same thing. The first one implements an exclusive group. The second allows multiple check boxes to be acted on.

          or is there a built in way to do this?

          QButtonGroup -

          QButtonGroup provides an abstract container into which button widgets can be placed. It does not provide a visual representation of this container (see QGroupBox for a container widget), but instead manages the states of each of the buttons in the group.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          2
          • P poordev123

            @JonB Thanks for your answer, I appreciate it. One question I have though, by parent do you mean the groupbox that they are in? Or the actual parent object of the checkbox class from qt?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @poordev123 said in PyQT how to get all of the selected checkboxes:

            by parent do you mean the groupbox that they are in?

            Yes.

            Or you can go down @jeremy_k's QButtonGroup approach. Are your checkboxes indeed intended to be exclusive from your elif code?

            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