简体   繁体   中英

My GUI doesn't work (buttons..etc) when i call other QtWidget in the main Window

I made a GUI in Python using Qt designer and i translate it to Pyqt5, the architecture of my GUI is : a main window and when i click on one of menu bar item the central widget replaced by another QtWidget (which have another class), the problem is when i central widget is replaced the buttons doesn't work, and if i execute this QtWidget directly it works.

this is my main code :

    # -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtCore import QCoreApplication
import os
import testModelC


class Ui_DzSenti(object):

    def setupUi(self, DzSenti):
        DzSenti.setObjectName("DzSenti")
        DzSenti.resize(900, 500)
        self.centralwidget = QtWidgets.QWidget(DzSenti)
        self.centralwidget.setObjectName("centralwidget")
        #***************************************************************************************************
        self.centralwidget = QtWidgets.QWidget(DzSenti)
        self.centralwidget.setObjectName("centralwidget")
        DzSenti.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(DzSenti)
        self.menubar.setEnabled(True)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
        self.menubar.setNativeMenuBar(False)
        self.menubar.setObjectName("menubar")
        self.menuTeste = QtWidgets.QMenu(self.menubar)
        self.menuTeste.setObjectName("menuTeste")
        DzSenti.setMenuBar(self.menubar)
        self.actionTeste_de_mod_le = QtWidgets.QAction(DzSenti)
        self.actionTeste_de_mod_le.setObjectName("actionTeste_de_mod_le")
        self.menuTeste.addAction(self.actionTeste_de_mod_le)
        self.menubar.addAction(self.menuTeste.menuAction())
        self.retranslateUi(DzSenti)
        QtCore.QMetaObject.connectSlotsByName(DzSenti)

    def retranslateUi(self, DzSenti):
        _translate = QtCore.QCoreApplication.translate
        DzSenti.setWindowTitle(_translate("DzSenti", "DzSenti"))
        self.menuTeste.setTitle(_translate("DzSenti", "Teste"))
        self.actionTeste_de_mod_le.setText(_translate("DzSenti", "Teste de modèle"))
        self.actionTeste_de_mod_le.triggered.connect(self.move_testeModel)

    def move_testeModel(self):
        Form = QtWidgets.QWidget()
        ui = testModelC.Ui_testeModel()
        ui.setupUi(Form)
        DzSenti.setCentralWidget(Form)

        #Method 2 doesn't work either by calling the qtwidget in another window (dialog)
        '''testModel1 = QtWidgets.QWidget()
        ui = testModelC.Ui_testeModel()
        ui.setupUi(testModel1)
        self.dialog = testModel1
        self.dialog.show()'''

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    DzSenti = QtWidgets.QMainWindow()
    ui = Ui_DzSenti()
    ui.setupUi(DzSenti)
    DzSenti.show()
    sys.exit(app.exec_())

and this one of my QtWidget code :

   # -*- coding: utf-8 -*-

from PyQt5.QtCore import pyqtSlot
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox

class Ui_testeModel(object):

    def setupUi(self, testeModel):
        testeModel.setObjectName("testeModel")
        testeModel.resize(815, 460)
        self.texteTeste_plainTextEdit = QtWidgets.QPlainTextEdit(testeModel)
        self.texteTeste_plainTextEdit.setGeometry(QtCore.QRect(70, 80, 721, 201))
        self.texteTeste_plainTextEdit.setObjectName("texteTeste_plainTextEdit")
        self.label_13 = QtWidgets.QLabel(testeModel)
        self.label_13.setGeometry(QtCore.QRect(30, 50, 201, 17))
        self.label_13.setObjectName("label_13")
        self.label_9 = QtWidgets.QLabel(testeModel)
        self.label_9.setGeometry(QtCore.QRect(200, 10, 441, 31))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label_9.setFont(font)
        self.label_9.setAlignment(QtCore.Qt.AlignCenter)
        self.label_9.setObjectName("label_9")
        self.Button_vider = QtWidgets.QPushButton(testeModel)
        self.Button_vider.setGeometry(QtCore.QRect(500, 350, 181, 41))
        self.Button_vider.setObjectName("Button_voirPrecision")
        self.Button_categoriser = QtWidgets.QPushButton(testeModel)
        self.Button_categoriser.setGeometry(QtCore.QRect(180, 350, 171, 41))
        self.Button_categoriser.setObjectName("Button_categoriser")
        self.Button_categoriser.clicked.connect(self.wrapper)
        self.Button_vider.clicked.connect(self.wrapper2)
        self.retranslateUi(testeModel)
        QtCore.QMetaObject.connectSlotsByName(testeModel)

    def retranslateUi(self, testeModel):
        _translate = QtCore.QCoreApplication.translate
        testeModel.setWindowTitle(_translate("testeModel", "Tester le modèle"))
        self.label_13.setText(_translate("testeModel", "Entrer un texte :"))
        self.label_9.setText(_translate("testeModel", "Tester le modèle"))
        self.Button_vider.setText(_translate("testeModel", "Vider"))
        self.Button_categoriser.setText(_translate("testeModel", "Catégoriser"))

    def wrapper(self):
        self.texteTeste_plainTextEdit.setPlainText("Hello it work")

    def wrapper2(self):
        self.texteTeste_plainTextEdit.setPlainText("")

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    testeModel = QtWidgets.QWidget()
    ui = Ui_testeModel()
    ui.setupUi(testeModel)
    testeModel.show()
    sys.exit(app.exec_())

I actually don't know whats going on here.. but rewrite move_testeModel actually helps:

def move_testeModel(self):
    try:
        Form = QtWidgets.QWidget()
        ui = Ui_testeModel()
        ui.setupUi(Form)
        ui.Button_categoriser.clicked.connect(lambda :ui.wrapper())
        DzSenti.setCentralWidget(Form)
    except Exception as e:
        print(e)

This will connect the the Button_categoriser clicked signal twice and will magically fix the second button signal....

I think it might have something to do with the retranslateUI function. Hope someone else can point out what is the real problem.

After further research i found where is the problem, i just forgot using self , as in the following code in Ui_DzSenti class:

self.Form = QtWidgets.QWidget()
ui = testModelC.Ui_testeModel()
ui.setupUi(self.Form)
DzSenti.setCentralWidget(self.Form)

so the class will be like this :

    # -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtCore import QCoreApplication
import os
import testModelC


class Ui_DzSenti(object):

    def setupUi(self, DzSenti):
        DzSenti.setObjectName("DzSenti")
        DzSenti.resize(900, 500)
        self.centralwidget = QtWidgets.QWidget(DzSenti)
        self.centralwidget.setObjectName("centralwidget")
        #***************************************************************************************************
        self.centralwidget = QtWidgets.QWidget(DzSenti)
        self.centralwidget.setObjectName("centralwidget")
        DzSenti.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(DzSenti)
        self.menubar.setEnabled(True)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
        self.menubar.setNativeMenuBar(False)
        self.menubar.setObjectName("menubar")
        self.menuTeste = QtWidgets.QMenu(self.menubar)
        self.menuTeste.setObjectName("menuTeste")
        DzSenti.setMenuBar(self.menubar)
        self.actionTeste_de_mod_le = QtWidgets.QAction(DzSenti)
        self.actionTeste_de_mod_le.setObjectName("actionTeste_de_mod_le")
        self.menuTeste.addAction(self.actionTeste_de_mod_le)
        self.menubar.addAction(self.menuTeste.menuAction())
        self.retranslateUi(DzSenti)
        QtCore.QMetaObject.connectSlotsByName(DzSenti)

    def retranslateUi(self, DzSenti):
        _translate = QtCore.QCoreApplication.translate
        DzSenti.setWindowTitle(_translate("DzSenti", "DzSenti"))
        self.menuTeste.setTitle(_translate("DzSenti", "Teste"))
        self.actionTeste_de_mod_le.setText(_translate("DzSenti", "Teste de modèle"))
        self.actionTeste_de_mod_le.triggered.connect(self.move_testeModel)

    def move_testeModel(self):
        self.Form = QtWidgets.QWidget()
        ui = testModelC.Ui_testeModel()
        ui.setupUi(self.Form)
        DzSenti.setCentralWidget(self.Form)

        #Method 2 doesn't work either by calling the qtwidget in another window (dialog)
        '''testModel1 = QtWidgets.QWidget()
        ui = testModelC.Ui_testeModel()
        ui.setupUi(testModel1)
        self.dialog = testModel1
        self.dialog.show()'''

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    DzSenti = QtWidgets.QMainWindow()
    ui = Ui_DzSenti()
    ui.setupUi(DzSenti)
    DzSenti.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM