Pyside6 Signal to Slot in different class
-
Hi,
I have a class that creates Previous and Next buttons on a TabWidget.
I also have the standard class MainWindow created by QApplication.
My aim is to emit a signal from the Previous and Next Buttons that will be actioned within the MainWindow.
If I click either of these 2 buttons I get the expected "Previous Pressed" printed to console.But, nothing appears to function in the MainWindow.
I am sending a signal emit(64) for previous and emit(65) for next from the TabWidget Class file.I am fairly convinced that I have ingested every example, video, book reference to using Signals and Slots.
Here is my code. Code someone please show me where I am going wrong. Thank you.
Kind regards,
jB
... from PySide6.QtCore import ( Slot, Signal ) class MainWindow(QMainWindow, Ui_MainWindow): """ The MainWindow Class. """ def __init__(self, app) -> None: super().__init__() self.setupUi(self) # type: ignore self.app = app # declare an app member ... @Slot(int) def signals_received(self, sig) -> None: print(f"Signal Received {sig}") match sig: case 64: print("Number 64 Received") case 66: print("Number 65 Received") case _: print("Got a different Signal")
... from PySide6.QtCore import ( Slot, Signal ) class tab_initial_setup(QWidget, initial_ui.Ui_tab_initial_setup): signal_pb_previous = Signal(int) def __init__(self) -> None: super().__init__() self.setupUi(self) # type: ignore self.pushButton_navigation_previous.clicked.connect(self.button_pushed_previous) @Slot(int) def button_pushed_previous(self) -> None: print("Previous Clicked") self.signal_pb_previous.emit(64) print("Previous Signal Emitted") ...
I have no way of knowing if the problem lies within the Signal or the Slot. I have tried VSCode debug but nothing shows up. So is there a way of seeing if a Signal is actually being emitted?
So please, can anyone help me unravel this conundrum that is giving me sleepless nights.
Thanks and kind regards,
jB -
@britesc said in Pyside6 Signal to Slot in different class:
But, nothing appears to function in the MainWindow.
I am fairly convinced that I have ingested every example, video, book reference to using Signals and Slots.
From your reading you know that every slot must be connected to a signal. You do so, for example, for
pushButton_navigation_previous.clicked
. Where is yourconnect()
statement for yoursignal_pb_previous
signal? This has nothing to do whether the connection is across classes or not. -
@JonB
Thank you for your response.
I agree with you.
But, I think I have confused myself into a corner as to the semantics.
I just don't know how to write or where to put the connect.
In which class?
Could you please indicate what I should code and where I should put it.
After that my 101 should be completed.
Thank you and kind regards,
jB -
@britesc
Qt connections must be made between instances of the signalling class and the slot class.You do not show where, but somewhere you create an instance of
tab_initial_setup
class. (It might be in yourself.setupUi(self)
if you created it in Designer, I don't know; perhaps not sincetab_initial_setup
is its own class.). You need to find that.Let's say somewhere (in
MainWindow
orUi_MainWindow
orsetupUi()
) you have something likeself.tab = tab_initial_setup()
. Then you would need:self.tab.signal_pb_previous.connect(self.signals_received)
in somewhere like
MainWindow
's__init__()
method, after theself.tab = tab_initial_setup()
has executed. -