PyQt4 中的事件與信號
在這一節(jié)中,將會接觸到程序中事件和信號的 we will explore events and singnals occuring in applications. 事件
事件是gui程序中最重要的部分。事件是由用或系統(tǒng)發(fā)出的。當我們調用程序的
事件源 是發(fā)生狀態(tài)改變改變的對象。它產(chǎn)生事件。事件對象將事件源狀態(tài)變化封裝起來。事件目標是需要告知的對象。事件源對象將事件傳送給事件目標處理。
當我們調用 新 API
PyQt4.5 引入信號與槽的一種新的API 風格。 QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'), self.onClicked)
上邊是老式的API button.clicked.connect(self.onClicked) 這是新式的更符合python標準。 信號&槽
這是一個展示PyQt4中的信號與槽。 #!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we connect a signal
of a QtGui.QSlider to a slot
of a QtGui.QLCDNumber.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
lcd = QtGui.QLCDNumber(self)
sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(lcd)
vbox.addWidget(sld)
self.setLayout(vbox)
sld.valueChanged.connect(lcd.display)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Signal & slot')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
In our example, we display a sld.valueChanged.connect(lcd.display)
Here we connect a The sender is an object that sends a signal. The receiver is the object, that receives the signal. The slot is the method, that reacts to the signal.
Figure: Signal & slot
重載事件處理器PyQt4 事件處理通常要重載事件處理器。 #!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we reimplement an
event handler.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Event handler')
self.show()
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_Escape:
self.close()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在我們的例子里, 我們重載了 def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_Escape:
self.close()
如果按了esc鍵,程序結束。 事件發(fā)送者
有時候需要知道信號的發(fā)送者,為了方便使用PyQt4 有一個 #!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we determine the event sender
object.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
btn1 = QtGui.QPushButton("Button 1", self)
btn1.move(30, 50)
btn2 = QtGui.QPushButton("Button 2", self)
btn2.move(150, 50)
btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)
self.statusBar()
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Event sender')
self.show()
def buttonClicked(self):
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed')
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在我們例子里有兩個按鈕。在 buttonClicked()方法里我們判斷哪個按鈕被按下使用了 btn1.clicked.connect(self.buttonClicked) btn2.clicked.connect(self.buttonClicked) 兩個按鈕都被連接到了相同的槽函數(shù)。 def buttonClicked(self):
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed')
我們使用
Figure: Event sender
發(fā)出信號
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we show how to emit a
custom signal.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui, QtCore
class Communicate(QtCore.QObject):
closeApp = QtCore.pyqtSignal()
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.c = Communicate()
self.c.closeApp.connect(self.close)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Emit signal')
self.show()
def mousePressEvent(self, event):
self.c.closeApp.emit()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我們創(chuàng)建了一個名叫 class Communicate(QtCore.QObject):
closeApp = QtCore.pyqtSignal()
創(chuàng)建了一個 self.c = Communicate() self.c.closeApp.connect(self.close) 一個Communicate類的實例被創(chuàng)建。我們 def mousePressEvent(self, event):
self.c.closeApp.emit()
當在窗口上點擊鼠票時,
|
|
|