1、类型判定
(1)总体结构
(2)API操作
1 from PyQt5.Qt import * 2 3 class Window(QWidget): 4 def __init__(self): 5 super().__init__() 6 self.setWindowTitle("QObject的学习") 7 self.resize(500, 500) 8 self.setup_ui() 9 10 def setup_ui(self): 11 #self.QObject_ceshi() 12 self.QObject_类型判定() 13 14 def QObject_类型判定(self): 15 # # *********** 测试API**************开始 16 obj=QObject() 17 w=QWidget() 18 btn=QPushButton() 19 label=QLabel() 20 21 objs=[obj,w,btn,label] 22 for o in objs: 23 # print(o.isWidgetType()) 24 # print(o.inherits('QWidget')) 25 print(o.inherits('QPushBotton')) 26 # *********** 测试API**************结束 27 28 29 if __name__ == '__main__': 30 import sys 31 32 app=QApplication(sys.argv) 33 window=Window() 34 window.show() 35 sys.exit(app.exec_())
(3)案例
1 from PyQt5.Qt import * 2 3 class Window(QWidget): 4 def __init__(self): 5 super().__init__() 6 self.setWindowTitle("QObject的学习") 7 self.resize(500, 500) 8 self.setup_ui() 9 10 def setup_ui(self): 11 #self.QObject_ceshi() 12 self.QObject_类型判定() 13 14 def QObject_类型判定(self): 15 # # *********** 测试API**************开始 16 # obj=QObject() 17 # w=QWidget() 18 # btn=QPushButton() 19 # label=QLabel() 20 # 21 # objs=[obj,w,btn,label] 22 # for o in objs: 23 # # print(o.isWidgetType()) 24 # # print(o.inherits('QWidget')) 25 # print(o.inherits('QPushBotton')) 26 # *********** 测试API**************结束 27 28 # **************案例*****************开始 29 label = QLabel(self) 30 label.setText("社会社会社会") 31 label.move(100, 100) 32 33 label2 = QLabel(self) 34 label2.setText("哈哈哈哈") 35 label2.move(150, 150) 36 37 btn = QPushButton(self) 38 btn.setText("点我") 39 btn.move(200, 200) 40 41 for widget in self.children(): 42 # print(widget) 43 if widget.inherits("QLabel"): 44 # print("是") 45 widget.setStyleSheet('background-color:green') 46 # **************案例*****************结束 47 48 if __name__ == '__main__': 49 import sys 50 51 app=QApplication(sys.argv) 52 window=Window() 53 window.show() 54 sys.exit(app.exec_())
2、对象删除
(1)总体框架
(2)API操作
1 from PyQt5.Qt import * 2 3 class Window(QWidget): 4 def __init__(self): 5 super().__init__() 6 self.setWindowTitle("QObject的学习") 7 self.resize(500, 500) 8 self.setup_ui() 9 10 def setup_ui(self): 11 #self.QObject_ceshi() 12 self.QObject_对象删除() 13 14 def QObject_对象删除(self): 15 # *********** 测试API**************开始 16 obj1 = QObject() 17 self.obj1=obj1 18 obj2 = QObject() 19 obj3 = QObject() 20 21 obj3.setParent(obj2) 22 obj2.setParent(obj1) 23 24 obj1.destroyed.connect(lambda:print("obj1被释放了")) 25 obj2.destroyed.connect(lambda: print("obj2被释放了")) 26 obj3.destroyed.connect(lambda: print("obj3被释放了")) 27 28 # del obj2 29 obj2.deleteLater() # 稍后删除 30 print(obj1.children()) # 先完成再删除 31 # *********** 测试API**************结束 36 37 if __name__ == '__main__': 38 import sys 39 40 app=QApplication(sys.argv) 41 window=Window() 42 window.show() 43 sys.exit(app.exec_())
时间:2020-03-03 00:34:44
作者:931935931(QQ)