1, type determination
(1) the overall structure
(2) API operation
. 1 from PyQt5.Qt Import * 2 . 3 class the Window (the QWidget): . 4 DEF the __init__ (Self): . 5 Super (). The __init__ () . 6 self.setWindowTitle ( " QObject learning " ) . 7 self.resize (500, 500 ) . 8 self.setup_ui () . 9 10 DEF setup_ui (Self): . 11 # self.QObject_ceshi () 12 is self.QObject_ type determination () 13 is 14 DEF QObject_ type determination (Self): 15 # # API ************** *********** test begins 16 obj = QObject () . 17 W = the QWidget () 18 is BTN = the QPushButton () . 19 label = the QLabel () 20 is 21 is OBJS = [obj, W, BTN, label] 22 is for O in OBJS: 23 is # Print (o.isWidgetType ()) 24 # Print (o.inherits ( 'the QWidget')) 25 Print (O .inherits ( ' QPushBotton ' )) 26 # *********** test API ************** end 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) Case
. 1 from PyQt5.Qt Import * 2 . 3 class the Window (the QWidget): . 4 DEF the __init__ (Self): . 5 Super (). The __init__ () . 6 self.setWindowTitle ( " QObject learning " ) . 7 self.resize (500, 500 ) . 8 self.setup_ui () . 9 10 DEF setup_ui (Self): . 11 # self.QObject_ceshi () 12 is self.QObject_ type determination () 13 is 14 DEF QObject_ type determination (Self): 15 # # API ************** *********** test begins 16 # obj = QObject () . 17 # W = the QWidget () 18 is # BTN the QPushButton = ( ) . 19 # label = the QLabel () 20 is # 21 is # OBJS = [obj, W, BTN, label] 22 is # for O in OBJS: 23 is # # Print (o.isWidgetType ()) 24 # # Print (o.inherits ( 'the QWidget')) 25 # Print (o.inherits ( 'QPushBotton')) 26 is # *********** API ************** test end 27 28 # ************** case ***************** start 29 = label the QLabel (Self) 30 label.setText ( " Social Social Society " ) 31 is label.move (100, 100 ) 32 33 is label2 = the QLabel (Self) 34 is label2.setText ( " ha ha " ) 35 label2.move (150 , 150 ) 36 37 [ BTN = the QPushButton (Self) 38 is btn.setText ( " point I " ) 39 btn.move (200 is, 200 is ) 40 41 is for the 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, object deletion
(1) the general framework
(2) API operation
. 1 from PyQt5.Qt Import * 2 . 3 class the Window (the QWidget): . 4 DEF the __init__ (Self): . 5 Super (). The __init__ () . 6 self.setWindowTitle ( " QObject learning " ) . 7 self.resize (500, 500 ) . 8 self.setup_ui () . 9 10 DEF setup_ui (Self): . 11 # self.QObject_ceshi () 12 is self.QObject_ object deletion () 13 is 14 DEF QObject_ object deletion (Self): 15 # *********** API ************** test begins 16 OBJ1 = QObject () . 17 self.obj1 = OBJ1 18 is obj2 = QObject () . 19 OBJ3 = QObject () 20 is 21 is obj3.setParent (obj2) 22 is obj2.setParent (OBJ1) 23 is 24 obj1.destroyed.connect ( the lambda : Print ( " OBJ1 been released " )) 25 obj2.destroyed.connect ( the lambda : Print ( " obj2 been released " )) 26 obj3.destroyed.connect ( the lambda : Print ( " obj3 been released " )) 27 28 # del obj2 29 obj2.deleteLater () # delete later 30 Print (obj1.children ()) # to complete and then delete 31 # *********** test API ************** end 32 33 # ************** case **** ************* start 34 35 # ************** case ***************** end 36 37 [ IF the __name__ == ' __main__ ' : 38 import sys 39 40 app=QApplication(sys.argv) 41 window=Window() 42 window.show() 43 sys.exit(app.exec_())
Time: 2020-03-03 00:34:44
Author: 931935931 (QQ)