Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt4.QtCore import *
- from PyQt4.QtGui import *
- #Create our custom class for the checkboxes and textboxes:
- class CheckBox(QWidget):
- def __init__(self, parent=None):
- QWidget.__init__(self, parent)
- self.layout = QVBoxLayout()
- # Add the check box and radio buttons for the first field -sidewalk
- self.cb1 = QCheckBox('sidewalk')
- self.cb1_rb1 = QRadioButton('left')
- self.cb1_rb2 = QRadioButton('right')
- self.cb1_rb3 = QRadioButton('both')
- # Add the check box and radio button for the second field -highway
- self.cb2 = QCheckBox('highway')
- self.cb2_rb1 = QRadioButton('residential')
- self.cb2_rb2 = QRadioButton('secondary')
- # Initially, all radio buttons are inactive
- self.cb1_rb1.setEnabled(False)
- self.cb1_rb2.setEnabled(False)
- self.cb1_rb3.setEnabled(False)
- self.cb2_rb1.setEnabled(False)
- self.cb2_rb2.setEnabled(False)
- # Connect the status signals of the checkboxes to the check box methods:
- self.cb1.toggled.connect(self.cb1_active)
- self.cb2.toggled.connect(self.cb2_active)
- # Select the according roads.. One slot for both of the checkboxes
- self.cb1.toggled.connect(self.selectRoads)
- self.cb2.toggled.connect(self.selectRoads)
- #Add the widgets to the layout:
- self.layout.addWidget(self.cb1)
- self.layout.addWidget(self.cb1_rb1)
- self.layout.addWidget(self.cb1_rb2)
- self.layout.addWidget(self.cb1_rb3)
- # Checkbox 2 (highway)
- self.layout.addWidget(self.cb2)
- self.layout.addWidget(self.cb2_rb1)
- self.layout.addWidget(self.cb2_rb2)
- # Group the radio buttons based on the field
- self.sidewalk=QButtonGroup()
- self.highway=QButtonGroup()
- # Add the radio buttons to the groups accordingly
- self.sidewalk.addButton(self.cb1_rb1)
- self.sidewalk.addButton(self.cb1_rb2)
- self.sidewalk.addButton(self.cb1_rb3)
- self.highway.addButton(self.cb2_rb1)
- self.highway.addButton(self.cb2_rb2)
- #Set the layout:
- self.setLayout(self.layout)
- # First checkbox
- def cb1_active(self, on):
- if on:
- # Enable the radio buttons
- self.cb1_rb1.setEnabled(True)
- self.cb1_rb2.setEnabled(True)
- self.cb1_rb3.setEnabled(True)
- # Once a radio button is toggled, select the roads accordingly
- self.cb1_rb1.toggled.connect(self.selectRoads)
- self.cb1_rb2.toggled.connect(self.selectRoads)
- self.cb1_rb3.toggled.connect(self.selectRoads)
- else:
- # Radio buttons should be inactive:
- self.cb1_rb1.setEnabled(False)
- self.cb1_rb2.setEnabled(False)
- self.cb1_rb3.setEnabled(False)
- # Uncheck the checked radio button in order to provide a better display:
- #https://stackoverflow.com/questions/8689909/uncheck-radiobutton-pyqt4
- self.sidewalk.setExclusive(False)
- # Return to the default
- self.cb1_rb1.setChecked(False)
- self.cb1_rb2.setChecked(False)
- self.cb1_rb3.setChecked(False)
- self.sidewalk.setExclusive(True)
- # ----------------------------------------------------------------------------------
- # Second checkbox
- def cb2_active(self, on):
- if on:
- self.cb2_rb1.setEnabled(True)
- self.cb2_rb2.setEnabled(True)
- self.cb2_rb1.toggled.connect(self.selectRoads)
- self.cb2_rb2.toggled.connect(self.selectRoads)
- else:
- self.cb2_rb1.setEnabled(False)
- self.cb2_rb2.setEnabled(False)
- # Uncheck the radio buttons:
- self.highway.setExclusive(False)
- self.cb2_rb1.setChecked(False)
- self.cb2_rb2.setChecked(False)
- self.highway.setExclusive(True)
- def selectRoads(self):
- # Obtain the configuration of the buttons:
- cb1 = self.cb1.isChecked()
- cb2 = self.cb2.isChecked()
- cb1_rb1 = self.cb1_rb1.isChecked()
- cb1_rb2 = self.cb1_rb2.isChecked()
- cb1_rb3 = self.cb1_rb3.isChecked()
- cb2_rb1 = self.cb2_rb1.isChecked()
- cb2_rb2 = self.cb2_rb2.isChecked()
- print cb1, cb1_rb1, cb1_rb2, cb1_rb3, cb2, cb2_rb1, cb2_rb2
- buttons = CheckBox()
- buttons.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement