Pyqt 控件的信号槽事件定义方法

转载来自:http://my.oschina.net/midnite/blog/39399

Qt采用信号槽来设定UI界面上元素动作的事件绑定。自Qt4.5开始,引入了一个新的信号槽与事件绑定的方法。界面上控件可以发出的默认内容都已经由Qt库定义好了,我们只需要定义处理方法即可。

从Designer设计出的控件中会使用setObjectName设定控件的名字,这个名字就可以让我们快捷的定义指定信号槽对应的处理方法。通过@PyQt4.QtCore.pyqtslot()修饰方法可以完成这一动作。定义方法如下:

@PyQt4.QtCore.pyqtSlot(参数)     #这个参数请到SDK中找,写参数的类型即可
def on_对象名称_信号名称(self,参数):
#on接setObjectName设定的对象名字再接SDK中标出的信号名称,即可完成信号处理事件的定义
pass

这种定义方法同样适用与自定义的信号槽。

首先需要定义信号:

class MyWidget(QWidget):
Signal_NoParameters = PyQt4.QtCore.pyqtSignal() # 无参数信号
Signal_OneParameter = PyQt4.QtCore.pyqtSignal(int) # 一个参数(整数)的信号

然后定义信号的激发:

class MyWidget(QWidget):
def mousePressEvent(self, event):
self.Signal_NoParameters.emit() # 焕发无参数信号
self.Signal_OneParameter.emit(1) # 焕发一个参数(整数)的信号

定义槽函数:

class MyWidget(QWidget):
@PyQt4.QtCore.pyqtSlot()
def setValue_NoParameters(self):
pass
@PyQt4.QtCore.pyqtSlot(int)
def setValue_OneParameter(self,nIndex):
pass

之后需要将信号与槽进行连接:

widget.Signal_NoParameters.connect(self.setValue_NoParameters,Qt.QueuedConnection)
widget.Signal_OneParameter.connect(self.setValue_OneParameter,Qt.QueuedConnection)

emit发送信号

#-*-coding:utf-8-*-
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import * class MyButton(QtGui.QPushButton):
def __init__(self):
super(MyButton,self).__init__()
self.connect(self, QtCore.SIGNAL("clicked()"), self.myClicked)
self.connect(self, QtCore.SIGNAL("myclicked()"), self.showMsg)
def myClicked(self):
self.emit(QtCore.SIGNAL("myclicked()"))
def showMsg(self):
print('test') if __name__ == "__main__":
import sys
app = QApplication(sys.argv) #MyApplication
main_widget = MyButton()
main_widget.show()
sys.exit(app.exec_())

发送带参数的信号

#-*-coding:utf-8-*-
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import * class MyButton(QtGui.QPushButton):
def __init__(self, *args, **kwargs):
QtGui.QPushButton.__init__(self, *args, **kwargs)
self.connect(self, QtCore.SIGNAL("clicked()"), self.myClicked)
self.connect(self, QtCore.SIGNAL("myclicked(int)"), self.showMsg) def myClicked(self):
param=123
self.emit(QtCore.SIGNAL("myclicked(int)"),param)
def showMsg(self,a):
print(a) if __name__ == "__main__":
import sys
app = QApplication(sys.argv) #MyApplication
main_widget = MyButton()
main_widget.show()
sys.exit(app.exec_())

接收另外页面的槽

#!/usr/bin/python
#-*-coding:utf-8-*- from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.Qt import * class TitleWidget(QWidget):
def __init__(self, parent=None):
super(TitleWidget, self).__init__(parent)
self.skin_button = QPushButton('dailog')
self.connect(self.skin_button, SIGNAL("clicked()"), self, SIGNAL("showSkin()")) main_layout = QVBoxLayout()
main_layout.addWidget(self.skin_button)
self.setLayout(main_layout) # 主入口文件
class MainWidget(QWidget):
def __init__(self, parent=None):
super(MainWidget, self).__init__(parent)
self.title_widget = TitleWidget(self)
self.title_widget.setFixedHeight(100)
self.setMinimumSize(100, 100) main_layout = QVBoxLayout()
main_layout.addWidget(self.title_widget)
main_layout.setSpacing(0)
self.setLayout(main_layout)
self.connect(self.title_widget, SIGNAL("showSkin()"), self, SLOT('showSkinWidget()')) # 等同 self.connect(self.title_widget, SIGNAL("showSkin()"),self.showSkinWidget) self.title_widget.skin_button.clicked.connect(self.onclick)
self.connect(self,SIGNAL('myclicked()'),self.myclickeddd) def onclick(self):
print('a')
self.emit(SIGNAL("myclicked()")) def myclickeddd(self):
print('b') @pyqtSlot()
def showSkinWidget(self):
print('sdf') if __name__ == "__main__":
import sys
app = QApplication(sys.argv) #MyApplication
main_widget = MainWidget()
main_widget.show()
sys.exit(app.exec_())
上一篇:Tomcat发布项目时,浏览器地址栏图标的问题


下一篇:IOS 单例模式的写法