Oct-28-2020, 10:12 AM
Hi
I'm looking for designing a way to display various layouts in a window using PyQt5. To do that, I try to define these layouts and the corresponding slots in specific classes to avoid defining all these layouts and slots in a single class
I want to connect signals and slots through the «ConnectSlotsByName» method.
As a test, I wrote this script :
Only in «mainA1» does «connectSlotsByName» really connect the signal with the corresponding slot.
What should I code to have «mainA2» and «mainA3» correctly use «connectSlotsByName» ?
I'm looking for designing a way to display various layouts in a window using PyQt5. To do that, I try to define these layouts and the corresponding slots in specific classes to avoid defining all these layouts and slots in a single class
I want to connect signals and slots through the «ConnectSlotsByName» method.
As a test, I wrote this script :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
from PyQt5 import QtWidgets from PyQt5.QtCore import QMetaObject, pyqtSlot from PyQt5.QtWidgets import QMainWindow, QWidget, QPushButton from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout class A1(QtWidgets.QWidget): def __init__(auto): super (A1,auto).__init__() def init(auto): zc = QHBoxLayout() zc.addStretch() btn = QPushButton( "A1" ) btn.setObjectName( "quit" ) zc.addWidget(btn) zc.addStretch() vbl = QVBoxLayout(auto) vbl.addStretch() vbl.addLayout(zc) auto.show() QMetaObject.connectSlotsByName(auto) @pyqtSlot () def on_quit_clicked(auto): print ( "on_quit_clicked" , type (auto)) class A2(): def init(auto,Fenster): zc = QHBoxLayout() zc.addStretch() btn = QPushButton( "A2" ) btn.setObjectName( "quitter" ) zc.addWidget(btn) zc.addStretch() vbl = QVBoxLayout(Fenster) vbl.addStretch() vbl.addLayout(zc) Fenster.show() QMetaObject.connectSlotsByName(Fenster) @pyqtSlot () def on_quitter_clicked(auto): print ( "on_quitter_clicked" , type (auto)) class A3_1(QMainWindow): def __init__(auto): super (A3_1,auto).__init__() print ( "A3_1.__init__" , type (auto)) def init(auto): print ( "A3_1.init" , type (auto)) auto.setGeometry( 250 , 200 , 800 , 300 ) auto.setWindowTitle( 'A3' ) class A3_2(QWidget): def __init__(auto): super (A3_2,auto).__init__() print ( "A3_2.__init__" , type (auto)) def init(auto, Fenster): zc = QHBoxLayout() zc.addStretch() btn = QPushButton( "A3_a" ) btn.setObjectName( "leave" ) zc.addWidget(btn) zc.addStretch() auto.btn = QPushButton( "A3_b" ) auto.btn.setObjectName( "autoleave" ) zc.addWidget(auto.btn) zc.addStretch() vbl = QVBoxLayout(auto) vbl.addStretch() vbl.addLayout(zc) Fenster.setCentralWidget(auto) QMetaObject.connectSlotsByName(Fenster) @pyqtSlot () def on_leave_clicked(auto): print ( "on_leave_clicked" , type (auto)) @pyqtSlot () def on_autoleave_clicked(auto): print ( "on_autoleave_clicked" , type (auto)) def mainA1(args): app = QtWidgets.QApplication(args) auteur = A1() auteur.init() app. exec () return 0 def mainA2(args): app = QtWidgets.QApplication(args) Fenster = QtWidgets.QWidget() auteur = A2() auteur.init(Fenster) Fenster.show() app. exec () return 0 def mainA3(args): app = QtWidgets.QApplication(args) Fenster = A3_1() Fenster.init() print ( type (Fenster)) auteur = A3_2() auteur.init(Fenster) Fenster.show() app. exec () return 0 if __name__ = = '__main__' : import sys sys.exit(mainA1(sys.argv)) # sys.exit(mainA2(sys.argv)) # sys.exit(mainA3(sys.argv)) |
What should I code to have «mainA2» and «mainA3» correctly use «connectSlotsByName» ?