qtcore.signal不做任何事情

发布于 2025-02-03 17:25:43 字数 3610 浏览 2 评论 0原文

我是PYQT5的初学者,我很难使用QTCore。

当我按下按钮并切换显示的当前窗口小件时,我想发送信号。 运行代码时我没有任何错误,但是当我按下按钮什么都没发生,我想这是因为我在qtcore上做错了什么。信号

这是我的代码:

from PySide2 import QtCore, QtWidgets
from ui_Page_accueil import Ui_MainWindow
from ui_NouvelleVerif import Ui_Dialog as Ui_NouvelleVerif
from ui_NouvelleVerifEssieux import Ui_Dialog as Ui_NouvelleVerifEssieux
import sys


class MainWindowUi(Ui_MainWindow):

    to_NouvelleVerif = QtCore.Signal()

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)

        #self.pushButton.clicked.connect(self.pushbutton_handler1)
        self.pushButton_2.clicked.connect(self.pushbutton_handler2)
        


    #def pushbutton_handler1(self):
    #    self.to_MainWindow.emit()

    def pushbutton_handler2(self):
        self.to_NouvelleVerif.emit()
        
 
    
class NouvelleVerifUi(QtWidgets.QWidget, Ui_NouvelleVerif):

    to_MainWindow = QtCore.Signal()
    to_NouvelleVerifEssieux = QtCore.Signal()

    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)

        self.pushButton.clicked.connect(self.pushbutton_handler1)
        #self.pushButton_2.clicked.connect(self.pushbutton_handler2)
        self.pushButton_3.clicked.connect(self.pushbutton_handler3)


    def pushbutton_handler1(self):
        self.to_MainWindow.emit()

    #def pushbutton_handler2(self):
    #    self.switch_window.emit()

    def pushbutton_handler3(self):
        self.to_NouvelleVerifEssieux.emit()
        


class NouvelleVerifEssieuxUi(QtWidgets.QWidget, Ui_NouvelleVerifEssieux):

    to_NouvelleVerif = QtCore.Signal()

    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)

        self.pushButton.clicked.connect(self.pushbutton_handler1)


    def pushbutton_handler1(self):
        self.to_NouvelleVerif.emit()


class Controller :
    app = QtWidgets.QApplication(sys.argv)
    widget = QtWidgets.QStackedWidget()
    MainWindow = MainWindowUi()
    NouvelleVerif = NouvelleVerifUi() 
    NouvelleVerifEssieux = NouvelleVerifEssieuxUi()     


    
        
    

    def __init__(self):
        
        self.widget.addWidget(self.MainWindow)   # create an instance of the first page class and add it to stackedwidget
        self.widget.addWidget(self.NouvelleVerif)   # adding second page   
        self.widget.addWidget(self.NouvelleVerifEssieux)
        self.widget.setCurrentWidget(self.MainWindow)   # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)

        


    def show_MainWindow(self):
        self.NouvelleVerif = NouvelleVerifUi()
        
        self.NouvelleVerif.to_MainWindow.connect(self.show_MainWindow)
        
        self.widget.setCurrentWidget(self.MainWindow)

    def show_NouvelleVerif(self):
        self.MainWindow = MainWindowUi()
        self.NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
        
        self.MainWindow.to_NouvelleVerif.connect(self.show_NouvelleVerif)
        self.NouvelleVerifEssieux.to_NouvelleVerif.connect(self.show_NouvelleVerif)

        self.widget.setCurrentWidget(self.NouvelleVerif)
        
    def show_NouvelleVerifEssieux(self):
        self.NouvelleVerif = NouvelleVerifUi()

        self.NouvelleVerif.to_NouvelleVerifEssieux.connect(self.show_NouvelleVerifEssieux)

        self.widget.setCurrentWidget(self.NouvelleVerifEssieux)
    
            

def main():
     
    controller = Controller()
    controller.widget.show()
    sys.exit(controller.app.exec_())

if __name__ == '__main__':
    main()

I am a beginner with PyQt5 and I am having trouble to use QtCore.signal

I'd like to send a signal when I press my buttons and switch the current widget displayed.
I don't have any errors when I run my code but when I press the buttons nothing happen and I guess it is because I am doing something wrong with the QtCore.Signal

Here is my code :

from PySide2 import QtCore, QtWidgets
from ui_Page_accueil import Ui_MainWindow
from ui_NouvelleVerif import Ui_Dialog as Ui_NouvelleVerif
from ui_NouvelleVerifEssieux import Ui_Dialog as Ui_NouvelleVerifEssieux
import sys


class MainWindowUi(Ui_MainWindow):

    to_NouvelleVerif = QtCore.Signal()

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)

        #self.pushButton.clicked.connect(self.pushbutton_handler1)
        self.pushButton_2.clicked.connect(self.pushbutton_handler2)
        


    #def pushbutton_handler1(self):
    #    self.to_MainWindow.emit()

    def pushbutton_handler2(self):
        self.to_NouvelleVerif.emit()
        
 
    
class NouvelleVerifUi(QtWidgets.QWidget, Ui_NouvelleVerif):

    to_MainWindow = QtCore.Signal()
    to_NouvelleVerifEssieux = QtCore.Signal()

    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)

        self.pushButton.clicked.connect(self.pushbutton_handler1)
        #self.pushButton_2.clicked.connect(self.pushbutton_handler2)
        self.pushButton_3.clicked.connect(self.pushbutton_handler3)


    def pushbutton_handler1(self):
        self.to_MainWindow.emit()

    #def pushbutton_handler2(self):
    #    self.switch_window.emit()

    def pushbutton_handler3(self):
        self.to_NouvelleVerifEssieux.emit()
        


class NouvelleVerifEssieuxUi(QtWidgets.QWidget, Ui_NouvelleVerifEssieux):

    to_NouvelleVerif = QtCore.Signal()

    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)

        self.pushButton.clicked.connect(self.pushbutton_handler1)


    def pushbutton_handler1(self):
        self.to_NouvelleVerif.emit()


class Controller :
    app = QtWidgets.QApplication(sys.argv)
    widget = QtWidgets.QStackedWidget()
    MainWindow = MainWindowUi()
    NouvelleVerif = NouvelleVerifUi() 
    NouvelleVerifEssieux = NouvelleVerifEssieuxUi()     


    
        
    

    def __init__(self):
        
        self.widget.addWidget(self.MainWindow)   # create an instance of the first page class and add it to stackedwidget
        self.widget.addWidget(self.NouvelleVerif)   # adding second page   
        self.widget.addWidget(self.NouvelleVerifEssieux)
        self.widget.setCurrentWidget(self.MainWindow)   # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)

        


    def show_MainWindow(self):
        self.NouvelleVerif = NouvelleVerifUi()
        
        self.NouvelleVerif.to_MainWindow.connect(self.show_MainWindow)
        
        self.widget.setCurrentWidget(self.MainWindow)

    def show_NouvelleVerif(self):
        self.MainWindow = MainWindowUi()
        self.NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
        
        self.MainWindow.to_NouvelleVerif.connect(self.show_NouvelleVerif)
        self.NouvelleVerifEssieux.to_NouvelleVerif.connect(self.show_NouvelleVerif)

        self.widget.setCurrentWidget(self.NouvelleVerif)
        
    def show_NouvelleVerifEssieux(self):
        self.NouvelleVerif = NouvelleVerifUi()

        self.NouvelleVerif.to_NouvelleVerifEssieux.connect(self.show_NouvelleVerifEssieux)

        self.widget.setCurrentWidget(self.NouvelleVerifEssieux)
    
            

def main():
     
    controller = Controller()
    controller.widget.show()
    sys.exit(controller.app.exec_())

if __name__ == '__main__':
    main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停滞 2025-02-10 17:25:43

@musicamante,我不知道为什么我可以调用qtcore.signal即使他处于尚未调用的函数,也可以调用。我做了你所说的话,我意识到我在小部件上犯了另一个错误。

在控制器类中,它们是创建并添加到qstackedwidget 中的__ init __,但是我正在创建新的,并试图将它们设置为Currentwidget不将它们添加到qstackedwidget中。

@AlexpDev我想用pushButton ui导航,以这种方式开始:

mainwindowui开始

MainWindowUI pushButton_2.clicked --> set the current widget display to NouvelleVerifUI

NouvelleVerifUI pushButton_1.clicked --> set the current widget display back to MainWindowUI 

NouvelleVerifUI pushButton_3.clicked --> set the current widget display to NouvelleVerifEssieuxUI  

NouvelleVerifEssieuxUI pushButton_1.clicked --> set the current widget display back to NouvelleVerifUI  

,现在一切正常起初。

class Controller :
    app = QtWidgets.QApplication(sys.argv)
    widget = QtWidgets.QStackedWidget()
    MainWindow = MainWindowUi()
    NouvelleVerif = NouvelleVerifUi() 
    NouvelleVerifEssieux = NouvelleVerifEssieuxUi()        
 
    def __init__(self):
        
        self.widget.addWidget(self.MainWindow)   # create an instance of the first page class and add it to stackedwidget
        self.widget.addWidget(self.NouvelleVerif)   # adding second page   
        self.widget.addWidget(self.NouvelleVerifEssieux)
        self.widget.setCurrentWidget(self.MainWindow)   # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)
        
        
        self.NouvelleVerif.to_MainWindow.connect(self.show_MainWindow)
        self.MainWindow.to_NouvelleVerif.connect(self.show_NouvelleVerif)
        self.NouvelleVerifEssieux.to_NouvelleVerif.connect(self.show_NouvelleVerif)
        self.NouvelleVerif.to_NouvelleVerifEssieux.connect(self.show_NouvelleVerifEssieux)

    def show_MainWindow(self):
        self.widget.setCurrentWidget(self.MainWindow)

    def show_NouvelleVerif(self):
        self.widget.setCurrentWidget(self.NouvelleVerif)
        
    def show_NouvelleVerifEssieux(self):
        self.widget.setCurrentWidget(self.NouvelleVerifEssieux)

谢谢大家的宝贵时间

@musicamante I don't know why I though the QtCore.Signal could be call even if he was in a function which has not been called. I did what you said and I realise I made another mistake with the widgets.

In the controller class they are create and add to the QStackedWidget in the __init__, but I was creating new ones and trying to set them as CurrentWidget without adding them to the QStackedWidget.

@alexpdev I wanted to navigate through my three differents with pushButton UI this way :

Start with MainWindowUI

MainWindowUI pushButton_2.clicked --> set the current widget display to NouvelleVerifUI

NouvelleVerifUI pushButton_1.clicked --> set the current widget display back to MainWindowUI 

NouvelleVerifUI pushButton_3.clicked --> set the current widget display to NouvelleVerifEssieuxUI  

NouvelleVerifEssieuxUI pushButton_1.clicked --> set the current widget display back to NouvelleVerifUI  

Now everything work I did what you said @musicamante and I also use the UI created at first.

class Controller :
    app = QtWidgets.QApplication(sys.argv)
    widget = QtWidgets.QStackedWidget()
    MainWindow = MainWindowUi()
    NouvelleVerif = NouvelleVerifUi() 
    NouvelleVerifEssieux = NouvelleVerifEssieuxUi()        
 
    def __init__(self):
        
        self.widget.addWidget(self.MainWindow)   # create an instance of the first page class and add it to stackedwidget
        self.widget.addWidget(self.NouvelleVerif)   # adding second page   
        self.widget.addWidget(self.NouvelleVerifEssieux)
        self.widget.setCurrentWidget(self.MainWindow)   # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)
        
        
        self.NouvelleVerif.to_MainWindow.connect(self.show_MainWindow)
        self.MainWindow.to_NouvelleVerif.connect(self.show_NouvelleVerif)
        self.NouvelleVerifEssieux.to_NouvelleVerif.connect(self.show_NouvelleVerif)
        self.NouvelleVerif.to_NouvelleVerifEssieux.connect(self.show_NouvelleVerifEssieux)

    def show_MainWindow(self):
        self.widget.setCurrentWidget(self.MainWindow)

    def show_NouvelleVerif(self):
        self.widget.setCurrentWidget(self.NouvelleVerif)
        
    def show_NouvelleVerifEssieux(self):
        self.widget.setCurrentWidget(self.NouvelleVerifEssieux)

Thank you all for your time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文