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 :
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 :
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)) 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» ?
