Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PyQt (but possibly C++) Very simple signal/slot "transference"/encapsulation?

PyQt (but possibly C++) Very simple signal/slot "transference"/encapsulation?

Scheduled Pinned Locked Moved Solved Language Bindings
4 Posts 2 Posters 1.6k Views 2 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    This is a question for PyQt. However, I may be able to adapt a C++ solution, depnding on what it is....

    I inherited code using a QLineEdit. I have to change that into a (what I call) a "composite" widget, consisting of a QWidget which holds a QHBoxLayout which in turn holds the original QLineEdit, plus a QPushButton; the button leads to something which can populate the QLineEdit.

    I'm OK with the design, apart from signal/slot handling. The outside world used to go QLineEdit.editingFinished.connect(...). To encapsulate, I'd like it to go CompositeWidget.editingFinished.connect(...), rather than addressing the QLineEdit directly. So I want to simply "transfer" the existing editingFinished signal/slot from the QLineEdit to the CompositeWidget level, "transparently".

    This is for PyQt 5 only, not earlier versions. So far I've never had to use PyQt @ annotations (@pySignal/Slot or whatever they are), and I'm not sure I ought need to, given the definition in QLineEdit in QtWidgets.pyi is already as plain as

    def editingFinished(self) -> None: ...
    

    ​So, given that I regard minimal code as neat/desired, what is like the minimum I need to write​ to achieve this? I will need the outside world to be able to connect(), my widget needs to be able to emit() it (when the user has finished interacting via the button, widget populates the QLineEdit and needs that to raise editingFinished signal to the outside world). I think that's it!

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      From a C++ point of view: no problem. Signal chaining is indeed the recommended way to propagate in such a case. Just connect the original signal to your custom signal and you should be good to go.

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

      For PyQt, I have discovered the magic of pyqtSignal() function for the simplest "redirection". So the code looks like:

      class JDateEdit(QWidget):
      
          # *** THE NEXT LINE IS THE PyQt MAGIC.... ***
          # class variable for "editingFinished" signal
          editingFinished = QtCore.pyqtSignal()
      
          def initUI(self):
              # lineEdit holds the date
              self.lineEdit = QLineEdit(self)
              ...
      
              # connect self.lineEdit.editingFinished signal to self.editingFinished signal
              self.lineEdit.editingFinished.connect(self.editingFinished)
      
      
      de = JDateEdit()
      de.editingFinished.connect(...)
      
      

      Dunno how this compares to whatever in C++ ...

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        From a C++ point of view: no problem. Signal chaining is indeed the recommended way to propagate in such a case. Just connect the original signal to your custom signal and you should be good to go.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        JonBJ 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          From a C++ point of view: no problem. Signal chaining is indeed the recommended way to propagate in such a case. Just connect the original signal to your custom signal and you should be good to go.

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

          For PyQt, I have discovered the magic of pyqtSignal() function for the simplest "redirection". So the code looks like:

          class JDateEdit(QWidget):
          
              # *** THE NEXT LINE IS THE PyQt MAGIC.... ***
              # class variable for "editingFinished" signal
              editingFinished = QtCore.pyqtSignal()
          
              def initUI(self):
                  # lineEdit holds the date
                  self.lineEdit = QLineEdit(self)
                  ...
          
                  # connect self.lineEdit.editingFinished signal to self.editingFinished signal
                  self.lineEdit.editingFinished.connect(self.editingFinished)
          
          
          de = JDateEdit()
          de.editingFinished.connect(...)
          
          

          Dunno how this compares to whatever in C++ ...

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Same as before except that you will have two Q_SIGNAL in the connect statement if using the old version.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

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