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. Enable and disable list of checkboxes using pyqt loop
Forum Updated to NodeBB v4.3 + New Features

Enable and disable list of checkboxes using pyqt loop

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 3.7k Views 1 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.
  • P Offline
    P Offline
    poordev123
    wrote on 24 Sept 2021, 22:39 last edited by
    #1

    I have about 16 checkboxes on my gui. I also have a list of values and based on those values I only want to enable certain checkboxes. my checkboxes are all named

    cb_text_(this then varies from rf1a, rf2a, rf3a, rf4a, rf1b, rf2b...).
    

    I have a list of all of the names (rf1a, rf2a, rf3a,...) and am trying to say

    plist = ['rf1a', 'rf2a', rf3a'...]
    
    for i in plist:
        enable_str = 'cb_text_' + str(i)
        self.ui.enable_str.setEnabled(True)
    

    but python is telling me

    AttributeError: 'PySide2.QtWidgets.QWidget' object has no attribute 'enable_str'
    
    J 1 Reply Last reply 25 Sept 2021, 07:22
    0
    • P poordev123
      24 Sept 2021, 22:39

      I have about 16 checkboxes on my gui. I also have a list of values and based on those values I only want to enable certain checkboxes. my checkboxes are all named

      cb_text_(this then varies from rf1a, rf2a, rf3a, rf4a, rf1b, rf2b...).
      

      I have a list of all of the names (rf1a, rf2a, rf3a,...) and am trying to say

      plist = ['rf1a', 'rf2a', rf3a'...]
      
      for i in plist:
          enable_str = 'cb_text_' + str(i)
          self.ui.enable_str.setEnabled(True)
      

      but python is telling me

      AttributeError: 'PySide2.QtWidgets.QWidget' object has no attribute 'enable_str'
      
      J Offline
      J Offline
      JonB
      wrote on 25 Sept 2021, 07:22 last edited by JonB
      #3

      @poordev123 said in Enable and disable list of checkboxes using pyqt loop:

      I have a list of all of the names (rf1a, rf2a, rf3a,...) and am trying to say

      The available approaches depend on what you mean by "names" here:

      • You are talking about the name given to the checkbox widget in Designer, so that there are variables like self.ui.cb_text_rf1a.
      • You gave each checkbox an object name in its properties in Designer.

      Even Python does not allow to directly access a variable via a string of its name!

      You could go down @mrjj's way of accessing self.ui's attributes to find a variable by a string of its name if you want, but it's a bit ugly (at least IMHO).

      However, in the case of a group of checkboxes (or radiobuttons) there is an alternative approach supplied by Qt: QButtonGroup Class. This allows you to group all your checkboxes and access each/all of them via e.g. QButtonGroup.buttons(). This has existed for years. A post like https://forum.qt.io/topic/24027/qtdesigner-how-radio-button-group-together mentions how to use this from Designer:

      You can also create a button group explicitly for your radio buttons. Select the ones you want to group, and right click on one of them. Then, you'll find an option to "group" the buttons. If you click that, you will notice that a new QButtonGroup has appeared in your object browser in designer. You can select that button group to give it a name, make it exclusive, etc.

      Going back, if you do want to stick with your "array/list* for the checkboxes, it's much easier if you create a list of references to the checkboxes than strings of their variable names:

      cblist = [self.ui.cb_text_rf1a, self.ui.cb_text_rf2a, self.ui.cb_text_rf3a, ...]
      for cb in cblist:
          cb.setEnabled(True)
      

      It's a few more characters to type for each one, but avoids having to try to convert strings to variable names.

      Finally, since I'm here, there is another way of accessing a bunch of widgets without knowing anything about their variable (or object) names. Provided they all share some parent you can find all checkboxes at runtime from that parent downward. So let's say self is a QMainWindow and you want to visit every checkbox on the main window. Then:

      cblist = self.findChildren(QCheckBox)
      for cb in cblist:
          cb.setEnabled(True)
      
      P 1 Reply Last reply 27 Sept 2021, 13:48
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 25 Sept 2021, 06:41 last edited by
        #2

        Hi
        Im not sure you can do it that way as the "compiler" just sees the actual expression
        " self.ui.enable_str."
        and not what's inside enable_str

        however, did you try with
        getattr

        https://www.programiz.com/python-programming/methods/built-in/getattr

        1 Reply Last reply
        0
        • P poordev123
          24 Sept 2021, 22:39

          I have about 16 checkboxes on my gui. I also have a list of values and based on those values I only want to enable certain checkboxes. my checkboxes are all named

          cb_text_(this then varies from rf1a, rf2a, rf3a, rf4a, rf1b, rf2b...).
          

          I have a list of all of the names (rf1a, rf2a, rf3a,...) and am trying to say

          plist = ['rf1a', 'rf2a', rf3a'...]
          
          for i in plist:
              enable_str = 'cb_text_' + str(i)
              self.ui.enable_str.setEnabled(True)
          

          but python is telling me

          AttributeError: 'PySide2.QtWidgets.QWidget' object has no attribute 'enable_str'
          
          J Offline
          J Offline
          JonB
          wrote on 25 Sept 2021, 07:22 last edited by JonB
          #3

          @poordev123 said in Enable and disable list of checkboxes using pyqt loop:

          I have a list of all of the names (rf1a, rf2a, rf3a,...) and am trying to say

          The available approaches depend on what you mean by "names" here:

          • You are talking about the name given to the checkbox widget in Designer, so that there are variables like self.ui.cb_text_rf1a.
          • You gave each checkbox an object name in its properties in Designer.

          Even Python does not allow to directly access a variable via a string of its name!

          You could go down @mrjj's way of accessing self.ui's attributes to find a variable by a string of its name if you want, but it's a bit ugly (at least IMHO).

          However, in the case of a group of checkboxes (or radiobuttons) there is an alternative approach supplied by Qt: QButtonGroup Class. This allows you to group all your checkboxes and access each/all of them via e.g. QButtonGroup.buttons(). This has existed for years. A post like https://forum.qt.io/topic/24027/qtdesigner-how-radio-button-group-together mentions how to use this from Designer:

          You can also create a button group explicitly for your radio buttons. Select the ones you want to group, and right click on one of them. Then, you'll find an option to "group" the buttons. If you click that, you will notice that a new QButtonGroup has appeared in your object browser in designer. You can select that button group to give it a name, make it exclusive, etc.

          Going back, if you do want to stick with your "array/list* for the checkboxes, it's much easier if you create a list of references to the checkboxes than strings of their variable names:

          cblist = [self.ui.cb_text_rf1a, self.ui.cb_text_rf2a, self.ui.cb_text_rf3a, ...]
          for cb in cblist:
              cb.setEnabled(True)
          

          It's a few more characters to type for each one, but avoids having to try to convert strings to variable names.

          Finally, since I'm here, there is another way of accessing a bunch of widgets without knowing anything about their variable (or object) names. Provided they all share some parent you can find all checkboxes at runtime from that parent downward. So let's say self is a QMainWindow and you want to visit every checkbox on the main window. Then:

          cblist = self.findChildren(QCheckBox)
          for cb in cblist:
              cb.setEnabled(True)
          
          P 1 Reply Last reply 27 Sept 2021, 13:48
          3
          • J JonB
            25 Sept 2021, 07:22

            @poordev123 said in Enable and disable list of checkboxes using pyqt loop:

            I have a list of all of the names (rf1a, rf2a, rf3a,...) and am trying to say

            The available approaches depend on what you mean by "names" here:

            • You are talking about the name given to the checkbox widget in Designer, so that there are variables like self.ui.cb_text_rf1a.
            • You gave each checkbox an object name in its properties in Designer.

            Even Python does not allow to directly access a variable via a string of its name!

            You could go down @mrjj's way of accessing self.ui's attributes to find a variable by a string of its name if you want, but it's a bit ugly (at least IMHO).

            However, in the case of a group of checkboxes (or radiobuttons) there is an alternative approach supplied by Qt: QButtonGroup Class. This allows you to group all your checkboxes and access each/all of them via e.g. QButtonGroup.buttons(). This has existed for years. A post like https://forum.qt.io/topic/24027/qtdesigner-how-radio-button-group-together mentions how to use this from Designer:

            You can also create a button group explicitly for your radio buttons. Select the ones you want to group, and right click on one of them. Then, you'll find an option to "group" the buttons. If you click that, you will notice that a new QButtonGroup has appeared in your object browser in designer. You can select that button group to give it a name, make it exclusive, etc.

            Going back, if you do want to stick with your "array/list* for the checkboxes, it's much easier if you create a list of references to the checkboxes than strings of their variable names:

            cblist = [self.ui.cb_text_rf1a, self.ui.cb_text_rf2a, self.ui.cb_text_rf3a, ...]
            for cb in cblist:
                cb.setEnabled(True)
            

            It's a few more characters to type for each one, but avoids having to try to convert strings to variable names.

            Finally, since I'm here, there is another way of accessing a bunch of widgets without knowing anything about their variable (or object) names. Provided they all share some parent you can find all checkboxes at runtime from that parent downward. So let's say self is a QMainWindow and you want to visit every checkbox on the main window. Then:

            cblist = self.findChildren(QCheckBox)
            for cb in cblist:
                cb.setEnabled(True)
            
            P Offline
            P Offline
            poordev123
            wrote on 27 Sept 2021, 13:48 last edited by
            #4

            @JonB Hi, thank you for your answer. I am going to go with the final way that you mentioned. That is

            cblist = self.findChildren(QCheckBox)
            for cb in cblist:
                cb.setEnabled(True)
            

            That worked for me. Thank you very much, I appreciate it.

            1 Reply Last reply
            0

            1/4

            24 Sept 2021, 22:39

            • Login

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