python PyQt不同类之间的通信

sdnqo3pr  于 2023-04-28  发布在  Python
关注(0)|答案(2)|浏览(239)

我想在PyQt5中的两个布局(在我的例子中是两个类)之间共享数据。
如何将一个布局操作正确地连接到另一个类的布局?例如,每当我点击类Left上的按钮时,类Right上的旋转框就会增加。

class Main(QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        generalLayout = QHBoxLayout()
        centralWidget = QWidget()
        centralWidget.setLayout(generalLayout)
        self.setCentralWidget(centralWidget)

        left = Left()
        right = Right()

        generalLayout.addLayout(left)
        generalLayout.addLayout(right)
        self.setLayout(generalLayout)

class Left(QVBoxLayout):
    signal = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super().__init__()
        button = QPushButton("+ 1")
        button.clicked.connect(self.add)
        self.addWidget(button)

    def add(self):
        self.signal.emit()

class Right(QVBoxLayout):
    def __init__(self):
        super().__init__()
        spinbox = QSpinbox()
        self.addWidget(spinbox)
        # spinbox.connect(???) ???

def main():
    mainApp = QApplication([])
    mainWindow = Main()
    mainWindow.show()
    sys.exit(mainApp.exec())

if __name__ == '__main__':
    main()

如何在我的右类中订阅左信号?
也许还有另一个功能,就像Web应用程序中的主体和观察者一样?

bvn4nwqk

bvn4nwqk1#

你想捕获信号并将其绑定到你的正确组件(例如绑定到一个函数),这样当信号被抛出时,它将调用相应的函数
举个例子:

class Main(QMainWindow):
        def __init__(self, parent=None):
            super().__init__()
            generalLayout = QHBoxLayout()
            centralWidget = QWidget()
            centralWidget.setLayout(generalLayout)
            self.setCentralWidget(centralWidget)
    
            left = Left()
            right = Right() 
            left.signal.connect(right.increment_spinbox_size) // connect left signal to a funcction inside right that will increase the size of the spinbox
            generalLayout.addLayout(left)
            generalLayout.addLayout(right)
            self.setLayout(generalLayout)

class Right(QVBoxLayout):
        def __init__(self, initial_value):
            super().__init__()
            self.spinbox = QSpinBox()
            self.addWidget(self.spinbox)
    
        def increment_spinbox_size(self):
            current_value = self.spinbox.value()
            self.spinbox.setValue(current_value + 1)
zy1mlcev

zy1mlcev2#

我使用父主类中的lambda函数调用另一个函数,该函数将更改第二个窗口中声明的类的值,从那里我可以访问另一个文件中的变量。需要导入Secondwww.example www.example.com
www.example. com 文件:

from Settings import Variables #Important to import your file.py of new window to access variables from there.

class CSettings(QMainWindow): #I created the widget class before the function of the main class, which will refer to the new Window.
    def __init__(self, parent=None):
        super(CSettings, self).__init__(parent)

        self.window = QtWidgets.QMainWindow()  
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.window)
        self.window.show()

class MWindow(QMainWindow):
    def __init__(self):
        super(MWindow, self).__init__()

        self.setCentralWidget(self.Parent)

        navbar = QToolBar()
        self.addToolBar(navbar)

        Settings_btn = QAction('⌬', self)  #create an example button
        Settings_btn.triggered.connect(self.Settings) #importantly, the lambda function will be called when I click the button and it will call the Settings function.
        navbar.addAction(Settings_btn)

        self.show()

    def Settings(self):
    Variables.needExit=False #From here I can access and define variables from another .py file in the case Settings.py

www.example. com 文件:

class Variables(object): #The class that contains the variable.

    needExit=False

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    
      the remainder of the code...

相关问题