⇤ ← Revision 1 as of 2008-06-11 10:01:23
Size: 6190
Comment:
|
Size: 6195
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 4: | Line 4: |
''' 含有章节索引的中文 文章模板 ''' |
''' 含有章节索引的中文 文章模板 ''' |
Line 8: | Line 6: |
::-- ["zhuyj"] [[[DateTime(2008-06-11T10:01:23Z)]]] [[TableOfContents]] |
::-- ["zhuyj"] [[[DateTime(2008-06-11T10:01:23Z)]]] [[TableOfContents]] |
Line 13: | Line 10: |
Line 14: | Line 12: |
在PyQt4编程指导的这一部分,我们将探索事件和信号在程序里的存在。 | 在PyQt4编程指导的这一部分,我们将探索事件和信号在程序里的存在。 |
Line 17: | Line 17: |
'' Events are an important part in any GUI program. Events are generated by users or by the system. When we call the application's exec_() method, the application enters the main loop. The main loop fetches events and sends them to the objects. Trolltech has introduced a unique signal and slot mechanism.'' 事件是任何GUI程序中最重要的部分。事件由用户或者系统生成,当我们调用应用程序的exec_()方法时,应用程序进入了它的主循环,主循环获取事件并将他们送给对象处理。奇趣科技引入了一种独一无二的信号和插槽机制来处理事件。 |
'' Events are an important part in any GUI program. Events are generated by users or by the system. When we call the application's exec_() method, the application enters the main loop. The main loop fetches events and sends them to the objects. Trolltech has introduced a unique signal and slot mechanism.'' 事件是任何GUI程序中最重要的部分。事件由用户或者系统生成,当我们调用应用程序的exec_()方法时,应用程序进入了它的主循环,主循环获取事件并将他们送给对象处理。奇趣科技引入了一种独一无二的信号和插槽机制来处理事件。 |
Line 21: | Line 24: |
'' Signals are emitted, when users click on the button, drag a slider etc. Signals can be emitted also by the environment. For example, when a clock ticks. A slot is a method, that reacts to a signal. In python, a slot can be any python callable. '' | ''Signals are emitted, when users click on the button, drag a slider etc. Signals can be emitted also by the environment. For example, when a clock ticks. A slot is a method, that reacts to a signal. In python, a slot can be any python callable. '' |
Line 23: | Line 28: |
{{{#!python | {{{ #!python |
Line 25: | Line 32: |
Line 27: | Line 33: |
Line 30: | Line 35: |
Line 35: | Line 38: |
Line 37: | Line 39: |
Line 40: | Line 41: |
Line 44: | Line 44: |
Line 46: | Line 45: |
self.connect(slider, QtCore.SIGNAL('valueChanged(int)'), lcd, | self.connect(slider, QtCore.SIGNAL('valueChanged(int)'), lcd, |
Line 48: | Line 47: |
Line 50: | Line 48: |
Line 58: | Line 54: |
在这个例子中,我们显示一个lcd数字和一个滑块,我们可以通过拖拽滑块来改变lcs的数字显示。 | 在这个例子中,我们显示一个lcd数字和一个滑块,我们可以通过拖拽滑块来改变lcs的数字显示。 |
Line 63: | Line 61: |
这里我们将滑块的valueChanged()信号与lcd数字的display()插槽相关联。 '' The connect method has four parameters. The sender is an object that sends a signal. The signal is the signal, which is emitted. The receiver is the object, that receives the signal. Finally the slot is the method, that reacts to the signal.'' 连接方法有四个参数。sender是发送信号的对象,signal是它产生的信号,receiver是接收信号的对象,最后的slot是相应信号的方法。 |
|
Line 67: | Line 62: |
signals & slots | 这里我们将滑块的valueChanged()信号与lcd数字的display()插槽相关联。 '' The connect method has four parameters. The sender is an object that sends a signal. The signal is the signal, which is emitted. The receiver is the object, that receives the signal. Finally the slot is the method, that reacts to the signal.'' 连接方法有四个参数。sender是发送信号的对象,signal是它产生的信号,receiver是接收信号的对象,最后的slot是相应信号的方法。 . signals & slots |
Line 69: | Line 70: |
Line 71: | Line 73: |
Line 72: | Line 75: |
PyQt中的事件处理主要是通过对事件处理者的重载来进行的。 {{{#!python |
PyQt中的事件处理主要是通过对事件处理者的重载来进行的。 {{{ #!python |
Line 75: | Line 81: |
Line 77: | Line 82: |
Line 80: | Line 84: |
Line 84: | Line 87: |
Line 88: | Line 90: |
Line 93: | Line 93: |
Line 100: | Line 99: |
在这个例子里,我们重载了keyPressEvent()方法。 | 在这个例子里,我们重载了keyPressEvent()方法。 |
Line 107: | Line 108: |
如果我们按下ESC键,程序将关闭。 | 如果我们按下ESC键,程序将关闭。 |
Line 110: | Line 113: |
'' Objects created from QtCore.QObject can emit signals. If we click on the button, a clicked() signal is generated. In the following example we will see, how we can emit signals.'' 通过QtCore.QObject创建的对象可以发送信号。如果我们点击按钮,就会生成一个clicked()信号。在以下的例子里我们会看到如何发送一个信号。 {{{#!python |
'' Objects created from QtCore.QObject can emit signals. If we click on the button, a clicked() signal is generated. In the following example we will see, how we can emit signals.'' 通过QtCore.QObject创建的对象可以发送信号。如果我们点击按钮,就会生成一个clicked()信号。在以下的例子里我们会看到如何发送一个信号。 {{{ #!python |
Line 114: | Line 121: |
Line 116: | Line 122: |
Line 119: | Line 124: |
Line 124: | Line 127: |
Line 128: | Line 130: |
Line 131: | Line 132: |
Line 138: | Line 138: |
我们创建一个叫做closeEmitApp()的新的信号。这个信号在鼠标按下时产生。 | 我们创建一个叫做closeEmitApp()的新的信号。这个信号在鼠标按下时产生。 |
Line 144: | Line 146: |
通过emit()来产生一个信号。 | 通过emit()来产生一个信号。 |
Line 149: | Line 153: |
Line 151: | Line 156: |
含有章节索引的中文 文章模板
::-- ["zhuyj"] [DateTime(2008-06-11T10:01:23Z)] TableOfContents
1. Events and Signals in PyQt4
PyQt4中的事件和信号
In this part of the PyQt4 programming tutorial, we will explore events and singnals occuring in applications.
在PyQt4编程指导的这一部分,我们将探索事件和信号在程序里的存在。
1.1. Events
事件
Events are an important part in any GUI program. Events are generated by users or by the system. When we call the application's exec_() method, the application enters the main loop. The main loop fetches events and sends them to the objects. Trolltech has introduced a unique signal and slot mechanism.
事件是任何GUI程序中最重要的部分。事件由用户或者系统生成,当我们调用应用程序的exec_()方法时,应用程序进入了它的主循环,主循环获取事件并将他们送给对象处理。奇趣科技引入了一种独一无二的信号和插槽机制来处理事件。
1.2. Signals & Slots
信号和插槽
Signals are emitted, when users click on the button, drag a slider etc. Signals can be emitted also by the environment. For example, when a clock ticks. A slot is a method, that reacts to a signal. In python, a slot can be any python callable.
当用户点击按钮,拖动滑块等操作时会产生信号,同时环境也可以产生信号,比如时钟的信号。插槽是一种针对信号进行处理的方法。python中插槽可以是任何的python可调用部分。
1 #!/usr/bin/python
2 # sigslot.py
3 import sys
4 from PyQt4 import QtGui, QtCore
5 class SigSlot(QtGui.QWidget):
6 def __init__(self, parent=None):
7 QtGui.QWidget.__init__(self, parent)
8 self.setWindowTitle('signal & slot')
9 lcd = QtGui.QLCDNumber(self)
10 slider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
11 vbox = QtGui.QVBoxLayout()
12 vbox.addWidget(lcd)
13 vbox.addWidget(slider)
14 self.setLayout(vbox)
15 self.connect(slider, QtCore.SIGNAL('valueChanged(int)'), lcd,
16 QtCore.SLOT('display(int)') )
17 self.resize(250, 150)
18 app = QtGui.QApplication(sys.argv)
19 qb = SigSlot()
20 qb.show()
21 sys.exit(app.exec_())
In our example, we display an lcd number and a slider. We change the lcd number by dragging the slider.
在这个例子中,我们显示一个lcd数字和一个滑块,我们可以通过拖拽滑块来改变lcs的数字显示。
self.connect(slider, QtCore.SIGNAL('valueChanged(int)'), lcd, QtCore.SLOT('display(int)') )
Here we connect a valueChanged() signal of the slider to the display() slot of the lcd number.
这里我们将滑块的valueChanged()信号与lcd数字的display()插槽相关联。
The connect method has four parameters. The sender is an object that sends a signal. The signal is the signal, which is emitted. The receiver is the object, that receives the signal. Finally the slot is the method, that reacts to the signal.
- 连接方法有四个参数。sender是发送信号的对象,signal是它产生的信号,receiver是接收信号的对象,最后的slot是相应信号的方法。
signals & slots
Figure: signal & slot
1.3. Reimplementing event handler
事件处理重载
Events in PyQt are processed mainly by reimplementing event handlers .
PyQt中的事件处理主要是通过对事件处理者的重载来进行的。
1 #!/usr/bin/python
2 # escape.py
3 import sys
4 from PyQt4 import QtGui, QtCore
5 class Escape(QtGui.QWidget):
6 def __init__(self, parent=None):
7 QtGui.QWidget.__init__(self, parent)
8 self.setWindowTitle('escape')
9 self.resize(250, 150)
10 self.connect(self, QtCore.SIGNAL('closeEmitApp()'), QtCore.SLOT('close()') )
11 def keyPressEvent(self, event):
12 if event.key() == QtCore.Qt.Key_Escape:
13 self.close()
14 app = QtGui.QApplication(sys.argv)
15 qb = Escape()
16 qb.show()
17 sys.exit(app.exec_())
In our example, we reimplement the keyPressEvent() event handler.
在这个例子里,我们重载了keyPressEvent()方法。
def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Escape: self.close()
If we click the escape button, we close the application.
如果我们按下ESC键,程序将关闭。
1.4. Emitting signals
发送信号
Objects created from QtCore.QObject can emit signals. If we click on the button, a clicked() signal is generated. In the following example we will see, how we can emit signals.
通过QtCore.QObject创建的对象可以发送信号。如果我们点击按钮,就会生成一个clicked()信号。在以下的例子里我们会看到如何发送一个信号。
1 #!/usr/bin/python
2 # emit.py
3 import sys
4 from PyQt4 import QtGui, QtCore
5 class Emit(QtGui.QWidget):
6 def __init__(self, parent=None):
7 QtGui.QWidget.__init__(self, parent)
8 self.setWindowTitle('emit')
9 self.resize(250, 150)
10 self.connect(self, QtCore.SIGNAL('closeEmitApp()'), QtCore.SLOT('close()') )
11 def mousePressEvent(self, event):
12 self.emit(QtCore.SIGNAL('closeEmitApp()'))
13 app = QtGui.QApplication(sys.argv)
14 qb = Emit()
15 qb.show()
16 sys.exit(app.exec_())
We create a new signal called closeEmitApp(). This signal is emitted, during a mouse press event.
我们创建一个叫做closeEmitApp()的新的信号。这个信号在鼠标按下时产生。
def mousePressEvent(self, event): self.emit(QtCore.SIGNAL('closeEmitApp()'))
Emitting a signal with the emit() method.
通过emit()来产生一个信号。
self.connect(self, QtCore.SIGNAL('closeEmitApp()'), QtCore.SLOT('close()') )
Here we connect the manually created closeEmitApp() signal with the close() slot.
这里我们将我们手工产生的closeEmitApp()信号与close()插槽连接。