1、类型判定
(1)总体结构
(2)API操作
from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QObject的学习") self.resize(500, 500) self.setup_ui() def setup_ui(self): #self.QObject_ceshi() self.QObject_类型判定() def QObject_类型判定(self): # # *********** 测试API**************开始 obj=QObject() w=QWidget() btn=QPushButton() label=QLabel() objs=[obj,w,btn,label] for o in objs: # print(o.isWidgetType()) # print(o.inherits('QWidget')) print(o.inherits('QPushBotton')) # *********** 测试API**************结束 if __name__ == '__main__': import sys app=QApplication(sys.argv) window=Window() window.show() sys.exit(app.exec_())
(3)案例
from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QObject的学习") self.resize(500, 500) self.setup_ui() def setup_ui(self): #self.QObject_ceshi() self.QObject_类型判定() def QObject_类型判定(self): # # *********** 测试API**************开始 # obj=QObject() # w=QWidget() # btn=QPushButton() # label=QLabel() # # objs=[obj,w,btn,label] # for o in objs: # # print(o.isWidgetType()) # # print(o.inherits('QWidget')) # print(o.inherits('QPushBotton')) # *********** 测试API**************结束 # **************案例*****************开始 label = QLabel(self) label.setText("社会社会社会") label.move(100, 100) label2 = QLabel(self) label2.setText("哈哈哈哈") label2.move(150, 150) btn = QPushButton(self) btn.setText("点我") btn.move(200, 200) for widget in self.children(): # print(widget) if widget.inherits("QLabel"): # print("是") widget.setStyleSheet('background-color:green') # **************案例*****************结束 if __name__ == '__main__': import sys app=QApplication(sys.argv) window=Window() window.show() sys.exit(app.exec_())
2、对象删除
(1)总体框架
(2)API操作
from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QObject的学习") self.resize(500, 500) self.setup_ui() def setup_ui(self): #self.QObject_ceshi() self.QObject_对象删除() def QObject_对象删除(self): # *********** 测试API**************开始 obj1 = QObject() self.obj1=obj1 obj2 = QObject() obj3 = QObject() obj3.setParent(obj2) obj2.setParent(obj1) obj1.destroyed.connect(lambda:print("obj1被释放了")) obj2.destroyed.connect(lambda: print("obj2被释放了")) obj3.destroyed.connect(lambda: print("obj3被释放了")) # del obj2 obj2.deleteLater() # 稍后删除 print(obj1.children()) # 先完成再删除 # *********** 测试API**************结束 if __name__ == '__main__': import sys app=QApplication(sys.argv) window=Window() window.show() sys.exit(app.exec_())