Creating checkboxes

Checkboxes are closely related to radio buttons, in that they offer options around a single theme. However, unlike radio buttons, checkboxes can be selected or unselected. You can also select more than one checkbox at a time. In this recipe, we'll create a dialog with checkboxes and some textboxes to programmatically track which checkboxes are selected.

Getting ready

Open the QGIS Python Console by selecting the Plugins menu and then clicking on Python Console.

How to do it...

In this recipe, we'll use a class to manage the checkboxes and the textbox widgets, as follows:

  1. First, we import the GUI and QGIS core libraries:
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
  2. Next, we create our custom class for the checkboxes and textboxes:
    class CheckBox(QWidget):
      def __init__(self, parent=None):
          QWidget.__init__(self, parent)
    
  3. Next, we'll need a layout object to manage the placement of the widgets:
    self.layout = QVBoxLayout()
    
  4. Now, we'll add three checkboxes and three textboxes:
          self.cb1 = QCheckBox('Option 1')
          self.cb2 = QCheckBox('Option 2')
          self.cb3 = QCheckBox('Option 3')
          self.textbox1 = QLineEdit()
          self.textbox2 = QLineEdit()
          self.textbox3 = QLineEdit()
    
  5. Then, we'll connect the status signals of the checkboxes to the methods that we'll define later:
          self.cb1.toggled.connect(self.cb1_active)
          self.cb2.toggled.connect(self.cb2_active)
          self.cb3.toggled.connect(self.cb3_active)
    
  6. Next, we must add the widgets to the layout:
          self.layout.addWidget(self.cb1)
          self.layout.addWidget(self.cb2)
          self.layout.addWidget(self.cb3)
          self.layout.addWidget(self.textbox1)
          self.layout.addWidget(self.textbox2)
          self.layout.addWidget(self.textbox3)
    
  7. Now, we set our custom class's layout to the layout we created:
          self.setLayout(self.layout)
    
  8. We then create the methods that change the textboxes each time a checkbox is toggled:
      # First checkbox
      def cb1_active(self, on):
          if on:
              self.textbox1.setText('Option 1 selected')
          else: self.textbox1.setText('') 
      # Second checkbox  
      def cb2_active(self, on):
          if on:
              self.textbox2.setText('Option 2 selected')
          else: self.textbox2.setText('') 
      # Third checkbox      
      def cb3_active(self, on):
          if on:
              self.textbox3.setText('Option 3 selected')
          else: self.textbox3.setText('')
    
  9. Now, we are ready to initialize our custom class and display the dialog:
    buttons = CheckBox()
    buttons.show()
    
  10. Toggle the checkboxes separately and simultaneously and then verify that the textboxes reflect the changes.

How it works...

Textboxes allow you to verify that you are programmatically catching the signal from the checkboxes as they are toggled. You can also use a single checkbox as a Boolean for an option with only two choices. When you run this recipe, the result should look similar to the following screenshot:

How it works...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset