简体   繁体   中英

PyQt Signal Slots conection between instances

How can I send the button clicked signal from SymbolPlotter to the statusbar in MainWindow and append the new message?

So basically when I click the button in my module Symbol Plotter I want to display a text in the status bar which is in main. Py in class MainWindow

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())


In SymbolPlotter, the signal is emitted

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

Based on musicamentes comment, reworked to the proper solution:

in SymbolPlotter.py module:

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)
            

in 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}')

i found a solution, i leave the post up, for someone who needs it, even though i dont know if my solution is the proper way, but it works:

in main.py i pass the statusbar object:

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

and in SymbolPlotter i use it: 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)

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