Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. from PyQt4.QtCore import *
  2. from PyQt4.QtGui import *
  3.  
  4. #Create our custom class for the checkboxes and textboxes:
  5. class CheckBox(QWidget):
  6. def __init__(self, parent=None):
  7. QWidget.__init__(self, parent)
  8. self.layout = QVBoxLayout()
  9.  
  10. # Add the check box and radio buttons for the first field -sidewalk
  11. self.cb1 = QCheckBox('sidewalk')
  12. self.cb1_rb1 = QRadioButton('left')
  13. self.cb1_rb2 = QRadioButton('right')
  14. self.cb1_rb3 = QRadioButton('both')
  15.  
  16. # Add the check box and radio button for the second field -highway
  17. self.cb2 = QCheckBox('highway')
  18. self.cb2_rb1 = QRadioButton('residential')
  19. self.cb2_rb2 = QRadioButton('secondary')
  20.  
  21. # Initially, all radio buttons are inactive
  22. self.cb1_rb1.setEnabled(False)
  23. self.cb1_rb2.setEnabled(False)
  24. self.cb1_rb3.setEnabled(False)
  25.  
  26. self.cb2_rb1.setEnabled(False)
  27. self.cb2_rb2.setEnabled(False)
  28.  
  29.  
  30. # Connect the status signals of the checkboxes to the check box methods:
  31. self.cb1.toggled.connect(self.cb1_active)
  32. self.cb2.toggled.connect(self.cb2_active)
  33. # Select the according roads.. One slot for both of the checkboxes
  34. self.cb1.toggled.connect(self.selectRoads)
  35. self.cb2.toggled.connect(self.selectRoads)
  36.  
  37. #Add the widgets to the layout:
  38. self.layout.addWidget(self.cb1)
  39. self.layout.addWidget(self.cb1_rb1)
  40. self.layout.addWidget(self.cb1_rb2)
  41. self.layout.addWidget(self.cb1_rb3)
  42. # Checkbox 2 (highway)
  43. self.layout.addWidget(self.cb2)
  44. self.layout.addWidget(self.cb2_rb1)
  45. self.layout.addWidget(self.cb2_rb2)
  46.  
  47. # Group the radio buttons based on the field
  48. self.sidewalk=QButtonGroup()
  49. self.highway=QButtonGroup()
  50. # Add the radio buttons to the groups accordingly
  51. self.sidewalk.addButton(self.cb1_rb1)
  52. self.sidewalk.addButton(self.cb1_rb2)
  53. self.sidewalk.addButton(self.cb1_rb3)
  54.  
  55. self.highway.addButton(self.cb2_rb1)
  56. self.highway.addButton(self.cb2_rb2)
  57.  
  58. #Set the layout:
  59. self.setLayout(self.layout)
  60.  
  61. # First checkbox
  62. def cb1_active(self, on):
  63. if on:
  64. # Enable the radio buttons
  65. self.cb1_rb1.setEnabled(True)
  66. self.cb1_rb2.setEnabled(True)
  67. self.cb1_rb3.setEnabled(True)
  68.  
  69. # Once a radio button is toggled, select the roads accordingly
  70. self.cb1_rb1.toggled.connect(self.selectRoads)
  71. self.cb1_rb2.toggled.connect(self.selectRoads)
  72. self.cb1_rb3.toggled.connect(self.selectRoads)
  73. else:
  74. # Radio buttons should be inactive:
  75. self.cb1_rb1.setEnabled(False)
  76. self.cb1_rb2.setEnabled(False)
  77. self.cb1_rb3.setEnabled(False)
  78.  
  79. # Uncheck the checked radio button in order to provide a better display:
  80. #https://stackoverflow.com/questions/8689909/uncheck-radiobutton-pyqt4
  81. self.sidewalk.setExclusive(False)
  82. # Return to the default
  83. self.cb1_rb1.setChecked(False)
  84. self.cb1_rb2.setChecked(False)
  85. self.cb1_rb3.setChecked(False)
  86.  
  87. self.sidewalk.setExclusive(True)
  88. # ----------------------------------------------------------------------------------
  89.  
  90.  
  91. # Second checkbox
  92. def cb2_active(self, on):
  93. if on:
  94. self.cb2_rb1.setEnabled(True)
  95. self.cb2_rb2.setEnabled(True)
  96.  
  97. self.cb2_rb1.toggled.connect(self.selectRoads)
  98. self.cb2_rb2.toggled.connect(self.selectRoads)
  99. else:
  100. self.cb2_rb1.setEnabled(False)
  101. self.cb2_rb2.setEnabled(False)
  102.  
  103. # Uncheck the radio buttons:
  104. self.highway.setExclusive(False)
  105. self.cb2_rb1.setChecked(False)
  106. self.cb2_rb2.setChecked(False)
  107. self.highway.setExclusive(True)
  108.  
  109. def selectRoads(self):
  110. # Obtain the configuration of the buttons:
  111. cb1 = self.cb1.isChecked()
  112. cb2 = self.cb2.isChecked()
  113.  
  114. cb1_rb1 = self.cb1_rb1.isChecked()
  115. cb1_rb2 = self.cb1_rb2.isChecked()
  116. cb1_rb3 = self.cb1_rb3.isChecked()
  117.  
  118. cb2_rb1 = self.cb2_rb1.isChecked()
  119. cb2_rb2 = self.cb2_rb2.isChecked()
  120.  
  121. print cb1, cb1_rb1, cb1_rb2, cb1_rb3, cb2, cb2_rb1, cb2_rb2
  122.  
  123. buttons = CheckBox()
  124. buttons.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement