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. Calling __init__() in QObject subclasses
Forum Updated to NodeBB v4.3 + New Features

Calling __init__() in QObject subclasses

Scheduled Pinned Locked Moved Unsolved Qt for Python
15 Posts 6 Posters 4.8k Views 4 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.
  • K KeirRice
    8 Oct 2019, 01:28

    Try this:

    import sys
    
    from PySide2.QtCore import QObject, Signal, Slot
    from PySide2.QtGui import QFont
    from PySide2.QtWidgets import QApplication, QPushButton
    
    class someClass(QObject):
        
        someSignal = Signal()
    
        def __init__(self):
            super(someClass, self).__init__()
            
            self.someSignal.connect(self.print_msg)
            try:
                self.someSignal.emit()
            except Exception as e:
                print("__init__:")
                print(e)
    
        @Slot()			
        def print_msg(self):
            print("lalalu")
    			
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
    		
        sc = someClass()
        button = QPushButton(text="Press me if you want to close the server")
        button.resize(640, 480)    
        button.setFont(QFont("Arial", 20, QFont.Bold))
        button.clicked.connect(button.close)
        button.show()
    
        sys.exit(app.exec_())
    
    • Signals have to be class variables to work.
    • You also need to make sure that the __init__ of the QObject is called. This is where pyside does the setup of the signals, trying to use the signal before setup wont work properly.
    • Lastly, make sure you use a Slot decorator on methods you want to connect into the signal system. It will appear to work without the decorator, but you will run into issues with the python class and c++ object lifetimes getting out of sync.
    J Offline
    J Offline
    JonB
    wrote on 12 Mar 2020, 10:23 last edited by kshegunov
    #1

    @KeirRice said in 'PySide2.QtCore.Signal' object has no attribute 'emit':

    You also need to make sure that the init of the QObject is called. This is where pyside does the setup of the signals, trying to use the signal before setup wont work properly.

    @KeirRice are you still around? :)

    This answer is indeed correct for PySide2. However, I don't understand what you mean by "make sure that the __init__ of the QObject is called". I do not define any def __init__(self) method of my class someClass(QObject), and it works fine. Just deriving from QObject (or from any class) calls its __init__() automatically.

    The only reason you need one in your example is because you want to do some other work there (connecting the signal in your case, which most people will do in the outside world, not in class's constructor). And then of course, like any derived class with its own __init__, you must call super().__init__().

    I see nothing at all special about defining/calling __init__() for the signal definition class. Do you have any comment (if you still exist :) )?

    [Edit: Locked as toxic and unproductive ~kshegunov]

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JonB
      wrote on 12 Mar 2020, 13:51 last edited by JonB 3 Dec 2020, 13:57
      #2

      @Denni-0
      The question has absolutely nothing to do with whether you use super().__init__() or baseClass.__init__(). Nothing to do with super(), irrelevant to the question, which is why that user says it's important "You also need to make sure that the __init__ of the QObject is called" in a class which defines a signal. That is the only question being asked.

      I am well aware of your opinion on super(), since you express it in nearly every post you make. And I don't agree with you, and nor do plenty of others.

      you should be trying to figure out what you are doing wrong rather than telling them they are wrong.

      Simply wrong. Did you read where I said

      This answer is indeed correct for PySide2.
      I do not define any def __init__(self) method of my class someClass(QObject), and it works fine.

      So it works fine without defining any __init__() method. And at no point did I say that poster was "wrong" in what he wrote, I merely asked him why he thinks it's important to do that. So don't tell me "I should be trying to figure out what you are doing wrong " when I'm not doing anything wrong.

      Nothing I have asked has anything to do with Python/PySide2 versus C++.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JonB
        wrote on 12 Mar 2020, 15:56 last edited by JonB 3 Dec 2020, 16:17
        #3

        @Denni-0

        of you misinformed state something that is absolutely wrong as the truth is within python you ought almost never call super().__init__() and instead use the original baseClass.__init__(self) instead.

        I don't agree, and nor do millions(?) of other Python3 programmers. But we'll have to move on, because discussing it gets us nowhere.

        Let's ask this. Here are two definitions of a derived class:

        First:

        class DerivedClass(BaseClass):
            pass
        

        Second:

        class DerivedClass(BaseClass):
            def __init__(self):
                BaseClass.__init__(self)
        

        Imagine that's it. There is nothing else, no other stuff. For example, there is no multi-inheritance, else the code would show it. Just that code. Not something else. (Note I've used BaseClass.__init__(), not even super().__init__(), so we don't have to wonder about super()).

        The question is: do these two behave any differently than each other, just as they stand? I say/claim/think/suspect they do not behave differently. If you do not define any __init__() in a derived class, it calls the base class's __init__() automatically (can be verified in a debugger), so I claim there is no difference. If you think there is a difference (I don't know whether you do), can you supply some actual, concrete reference which says so and explains precisely what it is?

        That's is all my question really is.

        1 Reply Last reply
        -1
        • J Offline
          J Offline
          JonB
          wrote on 12 Mar 2020, 16:37 last edited by JonB 3 Dec 2020, 16:38
          #4

          @Denni-0
          It's a very simple question. Nothing to do with slots, connections, potential issues, super, 3 issues, or anything else. Not to do with what one might or might not consider better style. There are no "underlying wrapper that has been created by someone else".

          Without answering about anything else, are you saying

          class DerivedClass(BaseClass):
              def __init__(self):
                  BaseClass.__init__(self)
          

          behaves differently from

          class DerivedClass(BaseClass):
              pass
          

          ? I'd like to know. If so, could you provide a reference so I can read up. Thanks.

          1 Reply Last reply
          -1
          • J Offline
            J Offline
            JonB
            wrote on 16 Mar 2020, 17:04 last edited by
            #5

            @Denni-0 said in 'PySide2.QtCore.Signal' object has no attribute 'emit':

            troll question

            I never ask "troll" questions. Life is too short.

            In my example I don't particularly intend BaseClass to be derived from QObject, else I would have stated that explicitly. Though it could be, if that makes any difference to the answer.

            I have since looked around and seen much code (both Qt and non-Qt) which does indeed not bother to declare an explicit __init__() constructor in a derived class if it does not need to do anything other than class the base class constructor. At the moment from reading around I am happy that the default behaviour of a derived class with no explicit __init__(), which is to call base class __init__() automatically, suffices. Unless you have a reference to the contrary?

            1 Reply Last reply
            -1
            • J Offline
              J Offline
              JonB
              wrote on 16 Mar 2020, 19:00 last edited by
              #6

              @Denni-0 said in 'PySide2.QtCore.Signal' object has no attribute 'emit':

              BTW if you had come to me respectfully with your question I would have been more than happy to have explained this to you in that manner but you chose to be basically disrespectful in your approach

              because you simply cannot comprehend the basic coding principles of Object Oriented programming let alone Function Oriented programming and I would suggest you go back to linear coding as you will cause a lot less harm with your programs.

              OK, if that's how you feel and speak let's call it a day. Nobody other than you speaks to people like this in this forum.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                JonB
                wrote on 16 Mar 2020, 22:07 last edited by JonB
                #7

                @Denni-0
                I have been careful to be polite in everything I have ever posted in response to you, always saying you have your opinions and others have theirs. You are just rude and abusive. Nobody other than you accuses others of lying, trolling, or the endless stream of insults you throw in your responses. I guess in all my career I have never come across anyone like you in a professional or technical environment. I still remember the first post I saw from you where you announced that @Pablo-J-Rogina was a "troll" and you would have him banned. It's a shame you are allowed to do so.

                We have nothing more to say to each other.

                kshegunovK 1 Reply Last reply 17 Mar 2020, 22:23
                1
                • J JonB
                  16 Mar 2020, 22:07

                  @Denni-0
                  I have been careful to be polite in everything I have ever posted in response to you, always saying you have your opinions and others have theirs. You are just rude and abusive. Nobody other than you accuses others of lying, trolling, or the endless stream of insults you throw in your responses. I guess in all my career I have never come across anyone like you in a professional or technical environment. I still remember the first post I saw from you where you announced that @Pablo-J-Rogina was a "troll" and you would have him banned. It's a shame you are allowed to do so.

                  We have nothing more to say to each other.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 17 Mar 2020, 22:23 last edited by
                  #8

                  @JonB said in Calling __init__() in QObject subclasses:

                  I guess in all my career I have never come across anyone like you in a professional or technical environment.

                  I have. I suppose you've just occupied a less hostile time and place in the universe.

                  I still remember the first post I saw from you where you announced that @Pablo-J-Rogina was a "troll" and you would have him banned.

                  Heh, that's actually rather funny.

                  It's a shame you are allowed to do so.

                  Personally, I'm very lenient, less personally - willfully treading on thin ice is going to get you into the freezing water, sooner or later.

                  Read and abide by the Qt Code of Conduct

                  ODБOïO 1 Reply Last reply 17 Mar 2020, 23:03
                  1
                  • kshegunovK kshegunov
                    17 Mar 2020, 22:23

                    @JonB said in Calling __init__() in QObject subclasses:

                    I guess in all my career I have never come across anyone like you in a professional or technical environment.

                    I have. I suppose you've just occupied a less hostile time and place in the universe.

                    I still remember the first post I saw from you where you announced that @Pablo-J-Rogina was a "troll" and you would have him banned.

                    Heh, that's actually rather funny.

                    It's a shame you are allowed to do so.

                    Personally, I'm very lenient, less personally - willfully treading on thin ice is going to get you into the freezing water, sooner or later.

                    ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on 17 Mar 2020, 23:03 last edited by ODБOï
                    #9

                    @kshegunov said in Calling __init__() in QObject subclasses:

                    get you into the freezing water

                    it is beneficial for your health

                    kshegunovK 1 Reply Last reply 17 Mar 2020, 23:29
                    0
                    • ODБOïO ODБOï
                      17 Mar 2020, 23:03

                      @kshegunov said in Calling __init__() in QObject subclasses:

                      get you into the freezing water

                      it is beneficial for your health

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 17 Mar 2020, 23:29 last edited by
                      #10

                      @LeLev said in Calling __init__() in QObject subclasses:

                      it is beneficial for your health

                      Ordinarily I'd agree, but the deadly sniffles is afoot, so I'd advise against it.

                      Read and abide by the Qt Code of Conduct

                      JKSHJ 1 Reply Last reply 18 Mar 2020, 03:23
                      0
                      • kshegunovK kshegunov
                        17 Mar 2020, 23:29

                        @LeLev said in Calling __init__() in QObject subclasses:

                        it is beneficial for your health

                        Ordinarily I'd agree, but the deadly sniffles is afoot, so I'd advise against it.

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on 18 Mar 2020, 03:23 last edited by JKSH
                        #11

                        @JonB said in Calling __init__() in QObject subclasses:

                        I don't understand what you mean by "make sure that the __init__ of the QObject is called". I do not define any def __init__(self) method of my class someClass(QObject), and it works fine. Just deriving from QObject (or from any class) calls its __init__() automatically.

                        While I don't have experience with PySide, I read @KeirRice's comment as saying: "IF you implement a custom __init__() method, THEN remember to explicitly call the base implementation of __init__()."

                        Does that sound right to you?

                        @kshegunov said in Calling __init__() in QObject subclasses:

                        the deadly sniffles

                        That's the first time I've heard it called that. It's a disturbingly cute name.

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        1
                        • kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on 18 Mar 2020, 16:09 last edited by
                          #12

                          @Denni-0 said in Calling __init__() in QObject subclasses:

                          @kshegunov the interesting thing I found is that JonB did not feel he was doing anything impolite as he feels every unrelated completely off topic manipulative comment he has made was simply him being polite.

                          What he feels or doesn't feel is beside the point. He asked an acceptable question, and even if you think it may've been manipulative, well sorry, you're just gonna have to eat it.

                          Which is ironic because that by nature is not a polite thing to do however I have no problems with giving back what I am given which is to say try to bully me and I get right in your grill

                          I don't grill. I have a very thick skin that's been growing thicker by merit of my living in eastern Europe. I don't care about political correctness, nor do I employ it, nor do I agree with it. I call it as I see it, if you don't like that then I suggest refraining from talking to me. Don't try to test the patience of the moderators group, we are decent enough bunch and allow a lot of freedom, but that doesn't mean disruptive aggressive behaviour is going to be tolerated for too long a time. This is not a discussion, mind you, I'm telling you how things are.

                          I am currently trying to find out what their definition of trolling is.

                          Trying to hijack the technical discussion on multiple occasions by arguing moderators' decisions is one form of trolling. Everyone is entitled to their opinion, but that doesn't mean everyone else is entitled to listening to their opinion, like with the "entitled" tone in the second part of your post. If you think this is some kind of a democracy where your opinion of what's trolling weighs more than say @SGaist's, then I must assure you you're sorely mistaken. Blatantly disrespectful tone and going around telling everybody you're smart and they're dumb doesn't fly well, neither in earning respect, nor in soliciting support for your point.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          4
                          • kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on 18 Mar 2020, 17:27 last edited by
                            #13

                            @Denni-0 said in Calling __init__() in QObject subclasses:

                            Yeah and your post was highly technical and spot on the original topic which is Calling init() in QObject subclasses --- NOT Best not to point fingers when you are extremely guilty yourself. Further its an extreme exaggeration "on multiple occasions?" or your math is really bad

                            Are we arguing? I told you already, this is not a discussion.

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 18 Mar 2020, 17:39 last edited by
                              #14

                              @Denni-0
                              Since we are talking facts.
                              Im the most friendly mod here and frankly, you are the first troll-like person i have seen here.

                              I understand you feel deeply about__INIT__ but you resort to name-calling and aggressive behavior
                              and insist that our long term users are trolling.

                              So can you please dial it down a notch?

                              1 Reply Last reply
                              3
                              • kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on 18 Mar 2020, 18:38 last edited by
                                #15

                                @Denni-0 said in Calling __init__() in QObject subclasses:

                                okay so you have ended this thread then?

                                Yes, metaphorically and literally.

                                Also I am assuming by your statements that you have made earlier that you have moderator power otherwise you would not be so abusive yourself?

                                When you assume ... and you know the rest. I've openly said already I am a moderator.

                                Read and abide by the Qt Code of Conduct

                                1 Reply Last reply
                                1

                                1/15

                                12 Mar 2020, 10:23

                                • Login

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