QCheckBox class
- isChecked()
- setTristate()
- setIcon ()
- setText()
- setChecked()
The signals sent by QCheckBox are as follows:
- clicked()
- stateChanged()
Examples
- Create a window based on the Dialog without Buttons template
- Drag out three Label parts and three Check Box parts, as shown
- Set the text properties of the first two Label components to Regular Pizza $ 10 and Select your extra toppings
- Set the objectName of the third Label component to labelAmount
- Set the text of the three Check Box parts to Extra Cheese $ 1, Extra Olives $ 1, and Extra Sausages $ 2
- Set the objectName of the three Check Box parts to checkBoxCheese, checkBoxOlives, checkBoxSausages
- Save as demoCheckBox1.ui file
- Use pyuic5 to generate demoCheckBox1.py file
- Create callCheckBox1.py file, call demoCheckBox1.py file, the code is as follows
import sys
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton
from cookbook_200419.demoCheckBox1 import *
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui=Ui_Dialog()
self.ui.setupUi(self)
self.ui.checkBoxCheese.stateChanged.connect(self.dispAmount)
self.ui.checkBoxOlives.stateChanged.connect(self.dispAmount)
self.ui.checkBoxSausages.stateChanged.connect(self.dispAmount)
self.show()
def dispAmount(self):
amount=10
if self.ui.checkBoxCheese.isChecked()==True:
amount=amount+1
if self.ui.checkBoxOlives.isChecked()==True:
amount=amount+1
if self.ui.checkBoxSausages.isChecked()==True:
amount=amount+2
self.ui.labelAmount.setText("Total amount for pizza is "+str(amount))
if __name__=="__main__":
app=QApplication(sys.argv)
w=MyForm()
w.show()
sys.exit(app.exec())