实例之间的 PyQt 信号槽连接

PyQt Signal Slots conection between instances

如何将按钮单击信号从 SymbolPlotter 发送到 MainWindow 中的状态栏并附加新消息?

所以基本上,当我在我的模块 Symbol Plotter 中单击按钮时,我想在 main 中的状态栏中显示一个文本。在 class MainWindow

中 Py

main.py:


import sys
from PyQt5 import QtWidgets as qtw

import SymbolPlotter as sp
import Stylesheet as st


class MainWindow(qtw.QMainWindow):

    def __init__(self, speed='1d1s'):
        super().__init__()
        
        # 1. create a tab widget in mainwindow
        tabs = qtw.QTabWidget(tabShape='triangular')
        tabs.setStyleSheet(st.TabBar)
        self.setCentralWidget(tabs)


        # create a status bar
        status_bar = qtw.QStatusBar()
        self.setStatusBar(status_bar)
        status_bar.showMessage('Ready')


        #1 tab to plot data in streaming fashion
        SymbolPlotter = sp.SymbolPlotterClass(symbol)  
        tabs.addTab(SymbolPlotter, f'Online Plotter: {symbol}')  


        #3 tab to save/update downloaded data to database
        DBStorage = dbs.DatabaseStorageClass()
        tabs.addTab(DBStorage, 'Database Storage')



        # End main UI code
        self.show()


    def UpdateStatusBarMessage(self, message='empty'):
        self.status_bar.showMessage('message')



if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    
    mw = MainWindow(symbol,speed='1d10ms')
    mw.setWindowTitle('Test')
    windows_style = qtw.QStyleFactory.create('Fusion')  
    app.setStyle(windows_style)
    sys.exit(app.exec())


在 SymbolPlotter 中,发出信号

SymbolPlotter.py

import sys

from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc
from PyQt5 import QtChart as qtch

from main import MainWindow as mw


import Stylesheet as st



class SymbolPlotterClass(qtw.QWidget):  
    def __init__(self, symbol):
        super().__init__()

       
        # create buttons
        button0 = qtw.QPushButton("Select Folder", self)  
        button0.setStyleSheet(st.b1) 
        
        #self.button0.clicked.connect(self.onPressed_btn0)
        button0.clicked.connect(lambda: mw.UpdateStatusBarMessage('test message'))

        
        # chart object and viewer 
        chart = qtch.QChart()  
        chartview = qtch.QChartView(chart)


        
        # create vertical layout object for page
        layout = qtw.QVBoxLayout()
        self.setLayout(layout)
        # add a horizontal sublayout
        layout.addWidget(chartview)

        sublayout = qtw.QHBoxLayout()
        layout.addLayout(sublayout)
        # add widgets to layout
        sublayout.addWidget(button0)
        

            

    # emit signal
    def onPressed_btn0(self):
        message = 'select folder pressed'
        #mw.UpdateStatusBarMessage()
        #self.submitted.emit()
        



if __name__ == '__main__':
    pass

根据 musicamentes 的评论,修改为正确的解决方案:

在 SymbolPlotter.py 模块中:

class SymbolPlotterClass(qtw.QWidget):  

    #A) create a signal outside init
    msgSignal = qtc.pyqtSignal(str)

    def __init__(self, symbol, time_iterator, message):
        super().__init__()

        ...


        # create button
        button0 = qtw.QPushButton("Select Folder", self)
        
        # connect 'button click' event to a methode
        button0.clicked.connect(self.onPressed_btn0)

        # add button to layout
        layout = qtw.QVBoxLayout()
        self.setLayout(layout)
        # add a horizontal sublayout
        layout.addWidget(button0)


    # emit signal in methode
    def onPressed_btn0(self):
        msg = 'button0 clicked'
        self.msgSignal.emit(msg)
            

在main.py中:

class MainWindow(qtw.QMainWindow):

    def __init__(self, symbol, folder='Desktop'):
        """MainWindow constructor. """
        super().__init__()
        
        ...

        # create a status bar
        status_bar = qtw.QStatusBar()
        self.setStatusBar(status_bar)
        status_bar.showMessage('Ready')


        # create the Plotter object 
        SymbolPlotter = sp.SymbolPlotterClass(symbol)  

        # connect the Signal from #A to the Slot/Function
        SymbolPlotter.msgSignal.connect(status_bar.showMessage)  

        # add tab to mainwindow
        tabs.addTab(SymbolPlotter, f'Online Plotter: {symbol}')

我找到了一个解决方案,我将 post 留给需要它的人,尽管我不知道我的解决方案是否是正确的方法,但它有效:

在 main.py 我传递了状态栏对象:

#1 tab to plot data in streaming fashion
SymbolPlotter = sp.SymbolPlotterClass(symbol, status_bar)  
tabs.addTab(SymbolPlotter, f'Online Plotter: {symbol}') 

我在 SymbolPlotter 中使用它: SymbolPlotter.py

class SymbolPlotterClass(qtw.QWidget):  
    def __init__(self, symbol, status_bar):
        super().__init__()

        self.status_bar = status_bar


        button0 = qtw.QPushButton("Select Folder", self);  
        button0.clicked.connect(self.onPressed_btn0)


    def onPressed_btn0(self):
        message = 'select folder pressed'
        self.status_bar.showMessage(message)