Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Pyside6 Signal to Slot in different class
Forum Updated to NodeBB v4.3 + New Features

Pyside6 Signal to Slot in different class

Scheduled Pinned Locked Moved Solved Qt for Python
pyside
5 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • britescB Offline
    britescB Offline
    britesc
    wrote on last edited by
    #1

    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

    mainwindow.py

    ...
    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")
    

    othercclass.py

    ...
    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

    Silver Haired Newbie to PySide6

    JonBJ 1 Reply Last reply
    0
    • britescB britesc

      @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

      britescB Offline
      britescB Offline
      britesc
      wrote on last edited by
      #4

      @JonB

      added to mainwidow.py

      initial_tab_setup.signal_pb_previous.connect(self.signals_received)
      

      and now get confirmation of emitted signal and slot receipt.
      Thank you for you help.
      Kind regards,
      jB

      Silver Haired Newbie to PySide6

      1 Reply Last reply
      0
      • britescB britesc

        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

        mainwindow.py

        ...
        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")
        

        othercclass.py

        ...
        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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @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 your connect() statement for your signal_pb_previous signal? This has nothing to do whether the connection is across classes or not.

        1 Reply Last reply
        0
        • britescB Offline
          britescB Offline
          britesc
          wrote on last edited by
          #3

          @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

          Silver Haired Newbie to PySide6

          britescB JonBJ 2 Replies Last reply
          0
          • britescB britesc

            @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

            britescB Offline
            britescB Offline
            britesc
            wrote on last edited by
            #4

            @JonB

            added to mainwidow.py

            initial_tab_setup.signal_pb_previous.connect(self.signals_received)
            

            and now get confirmation of emitted signal and slot receipt.
            Thank you for you help.
            Kind regards,
            jB

            Silver Haired Newbie to PySide6

            1 Reply Last reply
            0
            • britescB britesc

              @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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @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 your self.setupUi(self) if you created it in Designer, I don't know; perhaps not since tab_initial_setup is its own class.). You need to find that.

              Let's say somewhere (in MainWindow or Ui_MainWindow or setupUi()) you have something like self.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 the self.tab = tab_initial_setup() has executed.

              1 Reply Last reply
              0
              • britescB britesc has marked this topic as solved on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved