Creating radio buttons

Radio buttons are good for user input when you want the user to select an exclusive choice from a list of options, as opposed to checkboxes, which let a user select many or all of the options available. For longer lists of choices, a combobox is a better option. Once a radio button is selected, you can unselect it only by choosing another radio button.

Getting ready

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

How to do it...

Radio buttons are easier to manage as part of a class, so we'll create a custom class that also includes a textbox to view which radio button is selected. To do this, perform the following steps:

  1. First, we'll import both the GUI and the core QGIS libraries:
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
  2. Next, we'll create the RadioButton class and set up the radio buttons and the textbox:
    class RadioButton(QWidget):
      def __init__(self, parent=None):
          QWidget.__init__(self, parent)
    
  3. We must also define a layout to manage the placement of the widgets, as follows:
    self.layout = QVBoxLayout()
    self.rb1 = QRadioButton('Option 1')
    self.rb2 = QRadioButton('Option 2')
    self.rb3 = QRadioButton('Option 3')
    self.textbox = QLineEdit()
    
  4. Now, we'll connect the toggled signal of each radio button to the methods you'll define in just a moment, in order to detect when a radio button is selected:
    self.rb1.toggled.connect(self.rb1_active)
    self.rb2.toggled.connect(self.rb2_active)
    self.rb3.toggled.connect(self.rb3_active)
    
  5. Then, we'll add the radio buttons and the textbox to the layout:
    self.layout.addWidget(self.rb1)
    self.layout.addWidget(self.rb2)
    self.layout.addWidget(self.rb3)
    self.layout.addWidget(self.textbox)
    
  6. Now, we can define the layout for the custom widget we are building:
          self.setLayout(self.layout)
    
  7. Next, we can define the methods to indicate which radio button is selected. You can also define these options in a single method, but for a better understanding, three methods are easier:
    def rb1_active(self, on):
          if on:
              self.textbox.setText('Option 1 selected')
    def rb2_active(self, on):
          if on:
              self.textbox.setText('Option 2 selected')
    def rb3_active(self, on):
          if on:
             self.textbox.setText('Option 3 selected')
    
  8. We are now ready to initialize our class and display the radio buttons:
    buttons = RadioButton()
    buttons.show()
    
  9. Finally, click on each of the three radio buttons and verify that the text in the textbox changes to indicate that the radio button you clicked on is selected.

How it works...

Radio buttons are almost always grouped together as a single object because they are related options. Many GUI frameworks expose them as a single object in the API; however, Qt keeps them as separate objects for maximum control.

..................Content has been hidden....................

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