如何连接到PyQt中的父信号?

问题描述:

我有一个主窗口,看起来像这样:如何连接到PyQt中的父信号?

def __init__(self, parent = None): 

    QMainWindow.__init__(self, parent) 
    self.setupUi(self) 
    self.showMaximized() 

    menu=mainMenu.MainMenu() 

    classification=classificationMain.ClassificationMain() 

    self.stackedWidget.addWidget(menu) 
    self.stackedWidget.addWidget(classification) 

    self.stackedWidget.setCurrentWidget(menu) 
    self.stackedWidget.showFullScreen() 

    #connections 

    menu.pushButton.clicked.connect(self.showClassification) 
    classification.backButton.clicked.connect(self.showMainWindow) 

def showClassification(self): 
    self.stackedWidget.setCurrentIndex(3) 

def showMainWindow(self): 
    self.stackedWidget.setCurrentIndex(2) 

的MainWindows等待来自对话的复位信号。现在,Classification对话框中有另一个StackedWidget,因为它作为应用程序重要部分的主窗口。它看起来像:

class ClassificationMain(QDialog, Ui_Dialog): 

def __init__(self, parent = None): 

    QDialog.__init__(self, parent) 
    self.setupUi(self) 
    choose=choosePatient.ChoosePatient() 
    self.stackedWidget.addWidget(choose) 
    self.stackedWidget.setCurrentWidget(choose) 

现在,我想每一个从MainMenu的按钮“显示分类”被点击时重新加载内部ChoosePatient的数据,但现在的数据在线路类别= classificationMain.ClassificationMain只加载一次()的MainWindow。

我在想我必须在Main Menu中点击“Show Classification”按钮来连接ChoosePatient中的一个插槽,但是我需要一个MainMenu的实例,这是不可能的。

ChoosePatient的方法如何在每次点击“父”窗口中的按钮时执行? (也请告诉我,这是不是与PyQt的窗口工作,以正确的方式)

您需要将引用保存到您的组成部件,并且也在一定的公共方法暴露给父母:

class ClassificationMain(QDialog, Ui_Dialog): 

    def __init__(self, parent = None): 

     QDialog.__init__(self, parent) 
     self.setupUi(self) 
     self.chooseWidget=choosePatient.ChoosePatient() 
     self.stackedWidget.addWidget(self.chooseWidget) 
     self.stackedWidget.setCurrentWidget(self.chooseWidget) 

    def reloadPatients(self): 
     # whatever your operation should be on the ChoosePatient 
     self.chooseWidget.reload() 


# MAIN WINDOW 
def __init__(self, parent = None): 
    ... 
    self.classification=classificationMain.ClassificationMain() 
    self.stackedWidget.addWidget(self.classification) 
    ... 
    #connections 
    menu.pushButton.clicked.connect(self.showClassification) 

def showClassification(self): 
    self.stackedWidget.setCurrentIndex(3) 
    self.classification.reloadPatients() 

你也可以只跳过reloadPatients方法,并连接到ChoosePatient直接,如果你想:

def showClassification(self): 
    self.stackedWidget.setCurrentIndex(3) 
    self.classification.chooseWidget.reload() 

我个人的意见是,让您的自定义类很好地包裹起来的内部功能,所以你只需要对接它在自定义类上,而不是深入其内部。这样你就可以在不破坏主窗口的情况下改变它的工作方式。

+0

完美地工作。非常感谢你。 –