标签:ons img isp creat input 组件 span sign 编辑
1. Qt Creator -> File -> New File or Project -> Qt -> Qt Resource File ->添加但当前项目
2. 创建资源文件名res.qrc -> 右键 ->Open in Editor
3. 当前目录下新建文件夹images,将资源文件放在该文件夹下
4. add -> add Prefix ->设置前缀名icons -> 导入资源文件 -> 保存
5. 属性编辑器中的icon属性 -> Choose Resource
* 资源文件转换为py文件:pyrcc5 .\QtApp\res.qrc -o res_rc.py
# 自定义的信号类必须是Qobject类的子类 # 使用PyQt5.QtCore.pyqtSignal()为一个类定义新的信号 # pyqtSignal() # 信号定义为类属性 # connect()关联槽函数,disconnect()断开关联,emit()发射信号 import sys from PyQt5.QtCore import QObject, pyqtSlot, pyqtSignal class Human(QObject): # 定义一个带str类型的信号 nameChanged = pyqtSignal(str) # overload型信号有两种参数,int和str ageChanged = pyqtSignal([int],[str]) def __init__(self,name=‘mike‘,age=10,parent=None): super().__init__(parent) self.setAge(age) self.setName(name) def setAge(self,age): self.__age = age self.ageChanged.emit(self.__age) #发射int参数信号 if age<=18: ageInfo = ‘你是少年‘ elif 18<age<=35: ageInfo = ‘你是年轻人‘ elif 35<age<=55: ageInfo = ‘你是中年人‘ elif 55<age<=85: ageInfo = ‘你是老年人‘ elif age>85: ageInfo = ‘你是老寿星‘ self.ageChanged[str].emit(ageInfo) # 发射str参数信号 def setName(self,name): self.__name = name self.nameChanged.emit(self.__name) class Responsor(QObject): @pyqtSlot(int) def do_ageChanged_int(self,age): print(‘你的年龄是:‘,str(age)) @pyqtSlot(str) def do_ageChanged_str(self,ageInfo): print(ageInfo) @pyqtSlot(str) def do_nameChanged(self,name): print(‘Hello‘,name) if __name__ == ‘__main__‘: print(‘创建对象时:‘) boy = Human(‘Boy‘, 16) res = Responsor() # 信号关联槽 boy.nameChanged.connect(res.do_nameChanged) # overload信号,两个槽函数不能同名,关联时需要给信号加参数区分 boy.ageChanged.connect(res.do_ageChanged_int) boy.ageChanged[str].connect(res.do_ageChanged_str) print(‘建立关联:‘) boy.setAge(35) # 发射两个ageChanged信号 boy.setName(‘lily‘) # 发射nameChanged信号 # 断开关联 boy.ageChanged[str].disconnect(res.do_ageChanged_str) print(‘断开关联:‘) boy.setAge(20) # 发射两个ageChanged信号 boy.setName(‘lucy‘)
import sys from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtCore import pyqtSlot from PyQt5.QtGui import QIcon from ui_Widget import Ui_Widget from human import Human class QmyWidget(QWidget): def __init__(self,parent=None): super().__init__(parent) self.ui = Ui_Widget() # 构建UI对象 self.ui.setupUi(self) # 构造UI self.boy = Human(‘boy‘, 16) self.boy.nameChanged.connect(self.do_nameChanged) self.boy.ageChanged.connect(self.do_ageChanged_int) self.boy.ageChanged[str].connect(self.do_ageChanged_str) # 由connectSlotsByName() 自动与组件的信号关联的槽函数 def on_sliderSetAge_valueChanged(self, value): self.boy.setAge(value) def on_btnSetName_clicked(self): hisName = self.ui.editNameInput.text() self.boy.setName(hisName) # 自定义槽函数 def do_nameChanged(self,name): self.ui.editNameHello.setText(‘Hello,‘+name) @pyqtSlot(int) def do_ageChanged_int(self,age): self.ui.editAgeInt.setText(str(age)) @pyqtSlot(str) def do_ageChanged_str(self,info): self.ui.editAgeStr.setText(info) if __name__ == ‘__main__‘: app = QApplication(sys.argv) icon = QIcon(":/icons/images/app.ico") app.setWindowIcon(icon) form = QmyWidget() form.show() sys.exit(app.exec_())
Ui_Widget界面及属性布局
标签:ons img isp creat input 组件 span sign 编辑
原文地址:https://www.cnblogs.com/lnliyang/p/14087822.html