C1:Create Qt Components

这篇博客介绍了如何在Qt环境中创建各种组件,包括HelloWorld应用、RadioButton和Checkbox。通过详细的步骤,展示了如何实现这些组件并得到最终结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                     海南副教授陈晶优下台 ,shut down        you are rubbish ,you need study.

HelloWorld Application

Final Result

How to do it?

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.button_click.clicked.connect(self.dispmessage)
        self.show()
    def dispmessage(self):
        self.ui.label_res.setText("Hello,"+self.ui.name_tex.text())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

 

Radio Button Widget

Final Result

How to do it?

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.first.toggled.connect(self.dispFare)
        self.ui.business.toggled.connect(self.dispFare)
        self.ui.economy.toggled.connect(self.dispFare)
        self.show()
    def dispFare(self):
        fare = 0
        if self.ui.first.isChecked() == True:
            fare = 300
        if self.ui.business.isChecked() == True:
            fare = 250
        if self.ui.economy.isChecked() == True:
            fare = 100
        self.ui.res.setText("air fare is : "+str(fare))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

 

Group Radio Buttons

Final Result

How to do it?

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.M.toggled.connect(self.dispSelected)
        self.ui.L.toggled.connect(self.dispSelected)
        self.ui.XL.toggled.connect(self.dispSelected)
        self.ui.XXL.toggled.connect(self.dispSelected)
        self.ui.creditcard.toggled.connect(self.dispSelected)
        self.ui.netbanking.toggled.connect(self.dispSelected)
        self.ui.cash.toggled.connect(self.dispSelected)
        self.show()
    def dispSelected(self):
        selected1 = ""
        selected2 = ""
        if self.ui.M.isChecked() == True:
            selected1 = "M"
        if self.ui.L.isChecked() == True:
                    selected1 = "L"
        if self.ui.XL.isChecked() == True:
                    selected1 = "XL"
        if self.ui.XXL.isChecked() == True:
                    selected1 = "XXL"
        if self.ui.creditcard.isChecked() == True:
                    selected2 = "creditcard"
        if self.ui.netbanking.isChecked() == True:
                    selected2 = "netbanking"
        if self.ui.cash.isChecked() == True:
                    selected2 = "cash"
        self.ui.message.setText("chosen shirt size is "+selected1+" and payment method as "+selected2)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

 

Checkboxes Widget

Final Result

How to do it?

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.cheese.stateChanged.connect(self.dispAmount)
        self.ui.olives.stateChanged.connect(self.dispAmount)
        self.ui.sausage.stateChanged.connect(self.dispAmount)
        self.show()
    def dispAmount(self):
        amount = 10
        if self.ui.cheese.isChecked() == True:
            amount = amount+1
        if self.ui.olives.isChecked() == True:
            amount = amount+1
        if self.ui.sausage.isChecked() == True:
            amount = amount+2
        self.ui.message.setText("total amount for pizza is "+str(amount))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

 

Group Checkboxes

Final Result

How to do it?

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.choclatechip.stateChanged.connect(self.dispAmount)
        self.ui.cookie.stateChanged.connect(self.dispAmount)
        self.ui.choclatealmond.stateChanged.connect(self.dispAmount)
        self.ui.rockyroad.stateChanged.connect(self.dispAmount)
        self.ui.coffee.stateChanged.connect(self.dispAmount)
        self.ui.soda.stateChanged.connect(self.dispAmount)
        self.ui.tea.stateChanged.connect(self.dispAmount)
        self.show()
    def dispAmount(self):
        amount = 0
        if self.ui.choclatealmond.isChecked() == True:
            amount+=3
        if self.ui.choclatechip.isChecked() == True:
            amount+=4
        if self.ui.cookie.isChecked() == True:
            amount+=2
        if self.ui.rockyroad.isChecked() == True:
            amount+=5
        if self.ui.coffee.isChecked() == True:
            amount+=2
        if self.ui.soda.isChecked() == True:
            amount+=3
        if self.ui.tea.isChecked() == True:
            amount+=1
        self.ui.message.setText("Total amount is $"+str(amount))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值