import sys +from functools import partial from PyQt4.QtCore import * from PyQt4.QtGui import * -__version__ = "1.8.3" +__version__ = "1.8.4" class Form(QDialog): def __init__(self, parent=None): super().__init__(parent) - + + # simple part simple_partName = QLabel("Simple Widget") self.simple_label = QLabel("Hello, PyQt!") self.simple_labelFormat = "{}" @@ -40,7 +42,15 @@ class Form(QDialog): simple_layout.addWidget(self.simple_lineEidt, 2, 0, 1, 2) simple_layout.addWidget(self.simple_boldCheckBox, 3, 0) simple_layout.addLayout(simple_buttonLayout, 4, 0) + simple_layout.setRowStretch(5, 1) + + self.connect(self.simple_lineEidt, SIGNAL("returnPressed()"), + self.Simple_UpdateLabel) + self.connect(self.simple_boldCheckBox, SIGNAL("stateChanged(int)"), + self.Simple_LabelBold) + self.connect(simple_initButton, SIGNAL("clicked()"), self.Simple_Initialize) + # signal part signal_partName = QLabel("Signal Test") signal_spinCountLabel1 = QLabel("0") signal_spinCountLabel2 = QLabel("0") @@ -55,20 +65,68 @@ class Form(QDialog): signal_layout.addWidget(signal_spinBox) signal_layout.addStretch() + self.connect(signal_spinBox, SIGNAL("countup(QString)"), + signal_spinCountLabel1, SLOT("setText(QString)")) + self.connect(signal_spinBox, SIGNAL("countup(QString)"), + signal_spinCountLabel2.setText) + self.connect(signal_spinBox, SIGNAL("countup"), + signal_spinCountLabel3.setText) + + # connect part connect_partName = QLabel("Connect Test") self.connect_label = QLabel("Button Name") connect_button1 = QPushButton("Button1") connect_button2 = QPushButton("Button2") connect_button3 = QPushButton("Button3") - - connect_layout = QVBoxLayout() - connect_layout.addWidget(connect_partName) - connect_layout.addWidget(self.connect_label) - connect_layout.addWidget(connect_button1) - connect_layout.addWidget(connect_button2) - connect_layout.addWidget(connect_button3) - connect_layout.addStretch() - + + # partial connect + self.connect(connect_button1, SIGNAL("clicked()"), + partial(self.Connect_ButtonClick, "Button1")) + self.connect(connect_button2, SIGNAL("clicked()"), + partial(self.Connect_ButtonClick, "Button2")) + self.connect(connect_button3, SIGNAL("clicked()"), + partial(self.Connect_ButtonClick, "Button3")) + + connect_button4 = QPushButton("Button4") + connect_button5 = QPushButton("Button5") + connect_button6 = QPushButton("Button6") + + # lambda connect + self.connect(connect_button4, SIGNAL("clicked()"), + lambda name="Button4": self.Connect_ButtonClick(name)) + self.connect(connect_button5, SIGNAL("clicked()"), + lambda name="Button5": self.Connect_ButtonClick(name)) + self.connect(connect_button6, SIGNAL("clicked()"), + lambda name="Button6": self.Connect_ButtonClick(name)) + + connect_button7 = QPushButton("Button7") + connect_button8 = QPushButton("Button8") + connect_button9 = QPushButton("Button9") + + # sender connect + self.connect(connect_button7, SIGNAL("clicked()"), + self.Connect_SenderButtonClick) + self.connect(connect_button8, SIGNAL("clicked()"), + self.Connect_SenderButtonClick) + self.connect(connect_button9, SIGNAL("clicked()"), + self.Connect_SenderButtonClick) + + connect_layout = QGridLayout() + connect_layout.addWidget(connect_partName, 0, 0) + connect_layout.addWidget(self.connect_label, 1, 0) + connect_layout.addWidget(connect_button1, 2, 0) + connect_layout.addWidget(connect_button2, 3, 0) + connect_layout.addWidget(connect_button3, 4, 0) + connect_layout.addWidget(connect_button4, 2, 1) + connect_layout.addWidget(connect_button5, 3, 1) + connect_layout.addWidget(connect_button6, 4, 1) + connect_layout.addWidget(connect_button7, 2, 2) + connect_layout.addWidget(connect_button8, 3, 2) + connect_layout.addWidget(connect_button9, 4, 2) + connect_layout.setRowStretch(5, 1) + connect_layout.setColumnStretch(3, 1) + + # Main dialog part layout = QHBoxLayout() layout.addLayout(simple_layout) layout.addSpacing(30) @@ -77,27 +135,7 @@ class Form(QDialog): layout.addLayout(connect_layout) self.setLayout(layout) - - self.connect(self.simple_lineEidt, SIGNAL("returnPressed()"), - self.Simple_UpdateLabel) - self.connect(self.simple_boldCheckBox, SIGNAL("stateChanged(int)"), - self.Simple_LabelBold) - self.connect(simple_initButton, SIGNAL("clicked()"), self.Simple_Initialize) - self.connect(signal_spinBox, SIGNAL("countup(QString)"), - signal_spinCountLabel1, SLOT("setText(QString)")) - self.connect(signal_spinBox, SIGNAL("countup(QString)"), - signal_spinCountLabel2.setText) - self.connect(signal_spinBox, SIGNAL("countup"), - signal_spinCountLabel3.setText) - - self.connect(connect_button1, SIGNAL("clicked()"), - self.Connect_ButtonClick) - self.connect(connect_button2, SIGNAL("clicked()"), - self.Connect_ButtonClick) - self.connect(connect_button3, SIGNAL("clicked()"), - self.Connect_ButtonClick) - self.setWindowTitle("Main Dialog") def Simple_UpdateLabel(self): @@ -117,7 +155,10 @@ class Form(QDialog): self.simple_lineEidt.setText("This is LineEdit Widget") self.simple_label.setText("Hello, PyQt!") - def Connect_ButtonClick(self): + def Connect_ButtonClick(self, buttonName): + self.connect_label.setText(buttonName) + + def Connect_SenderButtonClick(self): button = self.sender() if not (button and isinstance(button, QPushButton)): return
PyQt 예제 1.8.4
이번 예제는 1.8에서 했던 시그널 구분 방식을 종합한 것이다. 단순히 내용을 종합했을 뿐이다.
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기