Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pyside6 how to disconnect connection?
QtWS25 Last Chance

Pyside6 how to disconnect connection?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgraphicssceneqgraphicsitemqobjesignal
34 Posts 5 Posters 12.5k Views
  • 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.
  • J Offline
    J Offline
    jeremy_k
    wrote on 30 Dec 2020, 23:47 last edited by
    #25

    The code I had issues with, using PyQt5, is:

    from PyQt5.QtCore import QObject
    obj1 = QObject()
    obj2 = QObject()
    obj1.destroyed.connect(lambda: obj2.deleteLater())
    del obj2
    del obj1
    

    Here, the capture is explicit, and the punishment is swift:

    Traceback (most recent call last):
    File "<stdin>", line 1, in <lambda>
    NameError: name 'obj2' is not defined
    Abort trap: 6
    

    Asking a question about code? http://eel.is/iso-c++/testcase/

    E S 2 Replies Last reply 30 Dec 2020, 23:51
    0
    • D Dariusz
      30 Dec 2020, 23:46

      @eyllanesc You are right, this works as it should. I must have more issues elsewhere and the "disconnect" signal solution I found must not be fixing the larger issue I have :- (

      E Offline
      E Offline
      eyllanesc
      wrote on 30 Dec 2020, 23:48 last edited by
      #26

      @Dariusz That is why I say that it provides an MRE, you will not expect the community to be crazy throwing papers on the wall waiting for one to stick.

      D 1 Reply Last reply 30 Dec 2020, 23:54
      0
      • J jeremy_k
        30 Dec 2020, 23:47

        The code I had issues with, using PyQt5, is:

        from PyQt5.QtCore import QObject
        obj1 = QObject()
        obj2 = QObject()
        obj1.destroyed.connect(lambda: obj2.deleteLater())
        del obj2
        del obj1
        

        Here, the capture is explicit, and the punishment is swift:

        Traceback (most recent call last):
        File "<stdin>", line 1, in <lambda>
        NameError: name 'obj2' is not defined
        Abort trap: 6
        
        E Offline
        E Offline
        eyllanesc
        wrote on 30 Dec 2020, 23:51 last edited by eyllanesc
        #27

        @jeremy_k First of all GC only removes the objects that have no references.

        The problem is very different, in principle when deleteLater is called it deletes the C ++ object and then a reference to the python object is eliminated, if it no longer has references the object would be eliminated as I already indicated, but in your particular case the lambda method has a scope so it has a reference and therefore the python object (the wrapper) will not be eliminated but the C ++ object

        E 1 Reply Last reply 30 Dec 2020, 23:55
        0
        • E eyllanesc
          30 Dec 2020, 23:48

          @Dariusz That is why I say that it provides an MRE, you will not expect the community to be crazy throwing papers on the wall waiting for one to stick.

          D Offline
          D Offline
          Dariusz
          wrote on 30 Dec 2020, 23:54 last edited by
          #28

          @eyllanesc said in Pyside6 how to disconnect connection?:

          @Dariusz That is why I say that it provides an MRE, you will not expect the community to be crazy throwing papers on the wall waiting for one to stick.

          Yes I understand your point, however, the app I'm working on has 150+ files and few thousand lines of code... Doing simple example usually "solve my issues", but my issues are not simple and solving them is quite "hard"... so I'm trying to ask for a "simple" answer to the question like... how to properly disconnect a signal if I have to do it manually and I don't want to rely on automatic qt logic... I guess I'm "old fashion" I like to manage my memory myself and python is a bit... "leme do it memememe" and I never know if he does it or not :- )

          E 1 Reply Last reply 30 Dec 2020, 23:58
          0
          • E eyllanesc
            30 Dec 2020, 23:51

            @jeremy_k First of all GC only removes the objects that have no references.

            The problem is very different, in principle when deleteLater is called it deletes the C ++ object and then a reference to the python object is eliminated, if it no longer has references the object would be eliminated as I already indicated, but in your particular case the lambda method has a scope so it has a reference and therefore the python object (the wrapper) will not be eliminated but the C ++ object

            E Offline
            E Offline
            eyllanesc
            wrote on 30 Dec 2020, 23:55 last edited by
            #29
            This post is deleted!
            1 Reply Last reply
            0
            • D Dariusz
              30 Dec 2020, 23:54

              @eyllanesc said in Pyside6 how to disconnect connection?:

              @Dariusz That is why I say that it provides an MRE, you will not expect the community to be crazy throwing papers on the wall waiting for one to stick.

              Yes I understand your point, however, the app I'm working on has 150+ files and few thousand lines of code... Doing simple example usually "solve my issues", but my issues are not simple and solving them is quite "hard"... so I'm trying to ask for a "simple" answer to the question like... how to properly disconnect a signal if I have to do it manually and I don't want to rely on automatic qt logic... I guess I'm "old fashion" I like to manage my memory myself and python is a bit... "leme do it memememe" and I never know if he does it or not :- )

              E Offline
              E Offline
              eyllanesc
              wrote on 30 Dec 2020, 23:58 last edited by
              #30

              @Dariusz Since you use thousands of files I suppose you use some system that manages versions like GIT and then rollback to the point that it does not generate problems, then implement the basic functionality in a minimal project where you can handle all the tests, then make a branch of the original project and at that time you just implemented the new functionality already tested.

              D 1 Reply Last reply 31 Dec 2020, 00:00
              0
              • E eyllanesc
                30 Dec 2020, 23:58

                @Dariusz Since you use thousands of files I suppose you use some system that manages versions like GIT and then rollback to the point that it does not generate problems, then implement the basic functionality in a minimal project where you can handle all the tests, then make a branch of the original project and at that time you just implemented the new functionality already tested.

                D Offline
                D Offline
                Dariusz
                wrote on 31 Dec 2020, 00:00 last edited by
                #31

                @eyllanesc Yes thats true. I would have to roll back 5 years and try to unpack the crazy system I build while learning how to code :- ). Well actually its what I'm doing now... a grand refactor rebuilding every piece of the app... Just trying to wrap my head around some odd crashes that I get with graphics views. Maybe I'm overdoing my memory managment and I should just... let it all be. hmmmm

                E 1 Reply Last reply 31 Dec 2020, 00:04
                0
                • D Dariusz
                  31 Dec 2020, 00:00

                  @eyllanesc Yes thats true. I would have to roll back 5 years and try to unpack the crazy system I build while learning how to code :- ). Well actually its what I'm doing now... a grand refactor rebuilding every piece of the app... Just trying to wrap my head around some odd crashes that I get with graphics views. Maybe I'm overdoing my memory managment and I should just... let it all be. hmmmm

                  E Offline
                  E Offline
                  eyllanesc
                  wrote on 31 Dec 2020, 00:04 last edited by
                  #32

                  @Dariusz You say: I build while learning how to code, you are crazy because no one should refactor code if they do not know the technology since you are going to make the same mistake as the previous programmer. Goodbye since your case is a hopeless case that will not have a solution since the forum is a site that has little space for a problem as big as the one you have.

                  1 Reply Last reply
                  0
                  • J jeremy_k
                    30 Dec 2020, 23:47

                    The code I had issues with, using PyQt5, is:

                    from PyQt5.QtCore import QObject
                    obj1 = QObject()
                    obj2 = QObject()
                    obj1.destroyed.connect(lambda: obj2.deleteLater())
                    del obj2
                    del obj1
                    

                    Here, the capture is explicit, and the punishment is swift:

                    Traceback (most recent call last):
                    File "<stdin>", line 1, in <lambda>
                    NameError: name 'obj2' is not defined
                    Abort trap: 6
                    
                    S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 31 Dec 2020, 11:30 last edited by
                    #33

                    Hi,

                    @jeremy_k said in Pyside6 how to disconnect connection?:

                    The code I had issues with, using PyQt5, is:
                    from PyQt5.QtCore import QObject
                    obj1 = QObject()
                    obj2 = QObject()
                    obj1.destroyed.connect(lambda: obj2.deleteLater())
                    del obj2
                    del obj1

                    One of the main issue here is that you destroy obj2 before obj1. So it's likely garbage collected before obj1 is deleted hence the call to deleteLater will fail.

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

                    J 1 Reply Last reply 31 Dec 2020, 19:24
                    2
                    • S SGaist
                      31 Dec 2020, 11:30

                      Hi,

                      @jeremy_k said in Pyside6 how to disconnect connection?:

                      The code I had issues with, using PyQt5, is:
                      from PyQt5.QtCore import QObject
                      obj1 = QObject()
                      obj2 = QObject()
                      obj1.destroyed.connect(lambda: obj2.deleteLater())
                      del obj2
                      del obj1

                      One of the main issue here is that you destroy obj2 before obj1. So it's likely garbage collected before obj1 is deleted hence the call to deleteLater will fail.

                      J Offline
                      J Offline
                      jeremy_k
                      wrote on 31 Dec 2020, 19:24 last edited by jeremy_k
                      #34

                      I guess I provided inadequate subtext for that example. It's documentation in a project I work on, explaining that qObject.signal.connect(lambda: anotherQObject.function(arg)) is unsafe in PyQt if anotherQObject might be destroyed before qObject. The same would happen in C++ with QObject::connect(qObject, &MyClass::signal, [&]() { anotherQObject->function(arg); });

                      In C++, one possible solution is to use QObject::Connect(sender, signal, context object, functor). To the best of my knowledge In PyQt5, and I suspect in PySide2/Qt for Python, there is no equivalent. You can write one by taking the connection object returned by object.signal.connect() and using it in a slot connected to anotherQObject.destroyed.

                      Asking a question about code? http://eel.is/iso-c++/testcase/

                      1 Reply Last reply
                      0

                      34/34

                      31 Dec 2020, 19:24

                      • Login

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