QRadioButtonクラス
- isChecked()
- setIcon()
- setText()
- setChecked()
送信できる信号は次のとおりです。
- toggled():この信号は、ボタンの状態が選択状態から非選択状態に、またはその逆に変化したときに発生します
- クリックされた():この信号は、ボタンがアクティブ化された(つまり、押して離した)か、そのショートカットキーが押されたときに発生します
- stateChanged():この信号は、ラジオボタンの状態が選択から非選択に、またはその逆に変化したときに発生します。
例
- ボタンのないダイアログテンプレートに基づいてウィンドウを作成する
- 2つのラベルパーツをドラッグして、ウィンドウに配置します
- 3つのラジオボタンウィジェットをドラッグして、ウィンドウの中央に配置します。
- 最初のラベルコンポーネントのテキストプロパティを[フライトタイプを選択]に設定し、2番目のラベルコンポーネントのテキストプロパティのデフォルト値を削除します。これは、テキスト値がプログラムコードによって入力されるためです。
- 3つのラジオボタンのテキストプロパティをFirst Class $ 150、Business Class $ 125、Economy Class $ 100として設定し、それらのobjectNameプロパティをradioButtonFirstClass、radioButtonBusinessClass、radioButtonEconomyClassとして設定します。
- demoRadioButton1.uiファイルとして保存
- pyuic5を使用して、demoRadioButton1.pyファイルを生成します。
- callRadioButton1.pyファイルを作成し、demoRadioButton1.pyファイルを呼び出します。コードは次のとおりです。
import sys
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200419.demoRadioButton1 import *
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui=Ui_Dialog()
self.ui.setupUi(self)
self.ui.radioButtonFirstClass.toggled.connect(self.dispFare)
self.ui.radioButtonBusinessClass.toggled.connect(self.dispFare)
self.ui.radioButtonEconomyClass.toggled.connect(self.dispFare)
self.show()
def dispFare(self):
fare=0
if self.ui.radioButtonFirstClass.isChecked()==True:
fare=150
if self.ui.radioButtonBusinessClass.isChecked()==True:
fare=125
if self.ui.radioButtonEconomyClass.isChecked()==True:
fare=100
self.ui.labelFare.setText("Air Fare is "+str(fare))
if __name__=="__main__":
app=QApplication(sys.argv)
w=MyForm()
w.show()
sys.exit(app.exec())