mirror of
https://github.com/maicss/PyQt5-Chinese-tutorial.git
synced 2025-01-24 21:12:54 +08:00
Update 事件和信号.md
This commit is contained in:
parent
c6a5cde564
commit
32ef5d826b
49
事件和信号.md
49
事件和信号.md
@ -137,12 +137,12 @@ def keyPressEvent(self, e):
|
|||||||
if e.key() == Qt.Key_Escape:
|
if e.key() == Qt.Key_Escape:
|
||||||
self.close()
|
self.close()
|
||||||
```
|
```
|
||||||
If we click the Escape button, the application terminates.
|
此时如果按下ESC键程序就会退出。
|
||||||
|
|
||||||
Event sender
|
## 事件发送
|
||||||
|
|
||||||
Sometimes it is convenient to know which widget is the sender of a signal. For this, PyQt5 has the sender() method.
|
|
||||||
|
|
||||||
|
有时候我们会想知道是哪个组件发出了一个信号,PyQt5里的`sender()`方法能搞定这件事。
|
||||||
|
```
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
@ -198,24 +198,29 @@ if __name__ == '__main__':
|
|||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
ex = Example()
|
ex = Example()
|
||||||
sys.exit(app.exec_())
|
sys.exit(app.exec_())
|
||||||
We have two buttons in our example. In the buttonClicked() method we determine which button we have clicked by calling the sender() method.
|
```
|
||||||
|
这个例子里有俩按钮,`buttonClicked()`方法决定了是哪个按钮能调用`sender()`方法。
|
||||||
|
```
|
||||||
btn1.clicked.connect(self.buttonClicked)
|
btn1.clicked.connect(self.buttonClicked)
|
||||||
btn2.clicked.connect(self.buttonClicked)
|
btn2.clicked.connect(self.buttonClicked)
|
||||||
Both buttons are connected to the same slot.
|
```
|
||||||
|
两个按钮都和同一个slot绑定。
|
||||||
|
```
|
||||||
def buttonClicked(self):
|
def buttonClicked(self):
|
||||||
|
|
||||||
sender = self.sender()
|
sender = self.sender()
|
||||||
self.statusBar().showMessage(sender.text() + ' was pressed')
|
self.statusBar().showMessage(sender.text() + ' was pressed')
|
||||||
We determine the signal source by calling the sender() method. In the statusbar of the application, we show the label of the button being pressed.
|
```
|
||||||
|
我们用调用`sender()`方法的方式决定了事件源。状态栏显示了被点击的按钮。
|
||||||
|
|
||||||
Event sender
|
程序展示:
|
||||||
Figure: Event sender
|
|
||||||
Emitting signals
|
|
||||||
|
|
||||||
Objects created from a QObject can emit signals. In the following example we will see how we can emit custom signals.
|
![event sender](./images/4-eventsender.png)
|
||||||
|
|
||||||
|
## 信号发送
|
||||||
|
|
||||||
|
`QObject`实例能发送事件信号。下面的例子是发送自定义的信号。
|
||||||
|
```
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
@ -268,18 +273,22 @@ if __name__ == '__main__':
|
|||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
ex = Example()
|
ex = Example()
|
||||||
sys.exit(app.exec_())
|
sys.exit(app.exec_())
|
||||||
We create a new signal called closeApp. This signal is emitted during a mouse press event. The signal is connected to the close() slot of the QMainWindow.
|
```
|
||||||
|
我们创建了一个叫closeApp的信号,这个信号会在鼠标按下的时候触发,事件与`QMainWindow`绑定。
|
||||||
|
```
|
||||||
class Communicate(QObject):
|
class Communicate(QObject):
|
||||||
|
|
||||||
closeApp = pyqtSignal()
|
closeApp = pyqtSignal()
|
||||||
A signal is created with the pyqtSignal() as a class attribute of the external Communicate class.
|
```
|
||||||
|
`Communicate`类创建了一个`pyqtSignal()`属性的信号。
|
||||||
|
```
|
||||||
self.c = Communicate()
|
self.c = Communicate()
|
||||||
self.c.closeApp.connect(self.close)
|
self.c.closeApp.connect(self.close)
|
||||||
The custom closeApp signal is connected to the close() slot of the QMainWindow.
|
```
|
||||||
|
`closeApp`信号`QMainWindow`的`close()`方法绑定。
|
||||||
|
```
|
||||||
def mousePressEvent(self, event):
|
def mousePressEvent(self, event):
|
||||||
|
|
||||||
self.c.closeApp.emit()
|
self.c.closeApp.emit()
|
||||||
When we click on the window with a mouse pointer, the closeApp signal is emitted. The application terminates.
|
```
|
||||||
|
当点击窗口的时候,closeApp信号发送,程序终止。
|
||||||
|
Loading…
x
Reference in New Issue
Block a user