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. Why i cant create instance of the simpliest custom class?
Forum Updated to NodeBB v4.3 + New Features

Why i cant create instance of the simpliest custom class?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 1.3k 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.
  • E Offline
    E Offline
    Engelard
    wrote on 10 Jun 2020, 23:15 last edited by Engelard 6 Nov 2020, 07:40
    #1

    I'm back to Qt after long time, and can't handle that old simple issue when creating custom class which inherits QObject. Class should be a worker class for QThread, but now, even when it's empty and just created it not allowing me create a simple object of my "worker" class.

    In the counstructor of my QMainWindow:

    tWorker = new timeWorker;
    

    alt text

    romnysoftware is a simple QMainWindow project, also just created, empty. The only thing i understand from that error handler - that .obj file of it is missing.

    1 Reply Last reply
    0
    • E Engelard
      11 Jun 2020, 09:58

      @JonB I'd show you completelly the same. Empty constructors of the worker class, but those working.

      alt text

      By empty i mean it is empty, no need to show worker::worker(){} stuff, i suppose it should be obvious.

      J Offline
      J Offline
      JonB
      wrote on 11 Jun 2020, 10:17 last edited by JonB 6 Nov 2020, 10:21
      #9

      @Engelard

      By empty i mean it is empty, no need to show worker::worker(){} stuff, i suppose it should be obvious.

      What you have elsewhere in your code might be obvious to you but is not obvious to us when we try to answer user questions. Do you mean that in addition to the .h file you show you do also have

      worker::worker()
      {
      }
      

      in the .cpp file?

      Cutting a long story short, if you feel your code is correct and you are getting irritated by these questions, have you deleted all files from the debug/release compilation directory and re-run qmake on your project? Especially if you, say, add or remove Q_OBJECT from the header, but also other cases, so make sure you have done this.

      Further, from the error message I see it mentions "file not found", don't know if that is relevant. And it is looking for a constructor which accepts a class QObject * parent, like https://doc.qt.io/qt-5/qobject.html#QObject does, which your timeWorker() does not.

      Thoughts for you, only trying to be helpful....

      1 Reply Last reply
      3
      • B Offline
        B Offline
        Bonnie
        wrote on 10 Jun 2020, 23:42 last edited by Bonnie 6 Oct 2020, 23:42
        #2

        Seems that you didn't implement the constructor of timeWorker.

        E 1 Reply Last reply 11 Jun 2020, 07:40
        1
        • B Bonnie
          10 Jun 2020, 23:42

          Seems that you didn't implement the constructor of timeWorker.

          E Offline
          E Offline
          Engelard
          wrote on 11 Jun 2020, 07:40 last edited by
          #3

          @Bonnie Yes, problem with that, but i peeked on my former projects, and in those, worker classes have empty constructors, and everything there looks the same. But there it works, and in this new it does not. Thats how my class looks like:

          alt text

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 11 Jun 2020, 07:46 last edited by
            #4

            timeWorker(); is not an empty constructor, it's a consructor declaration. You are telling the compiler to trust you that the constructor exists but if you don't actually implement it (empty or not) than the compiler complains because you lied to it.

            If you don't have to do anything in the constructor, however, the easiest way is typing using QObject::QObject; instead of decalring constructors, this tells the compiler to just use QObject constructors for your class

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            E 1 Reply Last reply 11 Jun 2020, 08:29
            3
            • V VRonin
              11 Jun 2020, 07:46

              timeWorker(); is not an empty constructor, it's a consructor declaration. You are telling the compiler to trust you that the constructor exists but if you don't actually implement it (empty or not) than the compiler complains because you lied to it.

              If you don't have to do anything in the constructor, however, the easiest way is typing using QObject::QObject; instead of decalring constructors, this tells the compiler to just use QObject constructors for your class

              E Offline
              E Offline
              Engelard
              wrote on 11 Jun 2020, 08:29 last edited by
              #5

              @VRonin Why i did'nt do anything like this before and everything was fine with empty constructors?

              J 1 Reply Last reply 11 Jun 2020, 09:04
              0
              • V Offline
                V Offline
                VRonin
                wrote on 11 Jun 2020, 08:44 last edited by
                #6

                Again. That's not an empty constructor.
                This is an empty constructor: timeWorker(){}
                This is a constructor declaration timeWorker();

                This is immutable from the dawn of C++

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                4
                • E Engelard
                  11 Jun 2020, 08:29

                  @VRonin Why i did'nt do anything like this before and everything was fine with empty constructors?

                  J Offline
                  J Offline
                  JonB
                  wrote on 11 Jun 2020, 09:04 last edited by
                  #7

                  @Engelard said in Why i cant create instance of the simpliest custom class?:

                  @VRonin Why i did'nt do anything like this before and everything was fine with empty constructors?

                  As @VRonin says, so why don't you show us what you did before which worked?

                  E 1 Reply Last reply 11 Jun 2020, 09:58
                  0
                  • J JonB
                    11 Jun 2020, 09:04

                    @Engelard said in Why i cant create instance of the simpliest custom class?:

                    @VRonin Why i did'nt do anything like this before and everything was fine with empty constructors?

                    As @VRonin says, so why don't you show us what you did before which worked?

                    E Offline
                    E Offline
                    Engelard
                    wrote on 11 Jun 2020, 09:58 last edited by
                    #8

                    @JonB I'd show you completelly the same. Empty constructors of the worker class, but those working.

                    alt text

                    By empty i mean it is empty, no need to show worker::worker(){} stuff, i suppose it should be obvious.

                    J 1 Reply Last reply 11 Jun 2020, 10:17
                    0
                    • E Engelard
                      11 Jun 2020, 09:58

                      @JonB I'd show you completelly the same. Empty constructors of the worker class, but those working.

                      alt text

                      By empty i mean it is empty, no need to show worker::worker(){} stuff, i suppose it should be obvious.

                      J Offline
                      J Offline
                      JonB
                      wrote on 11 Jun 2020, 10:17 last edited by JonB 6 Nov 2020, 10:21
                      #9

                      @Engelard

                      By empty i mean it is empty, no need to show worker::worker(){} stuff, i suppose it should be obvious.

                      What you have elsewhere in your code might be obvious to you but is not obvious to us when we try to answer user questions. Do you mean that in addition to the .h file you show you do also have

                      worker::worker()
                      {
                      }
                      

                      in the .cpp file?

                      Cutting a long story short, if you feel your code is correct and you are getting irritated by these questions, have you deleted all files from the debug/release compilation directory and re-run qmake on your project? Especially if you, say, add or remove Q_OBJECT from the header, but also other cases, so make sure you have done this.

                      Further, from the error message I see it mentions "file not found", don't know if that is relevant. And it is looking for a constructor which accepts a class QObject * parent, like https://doc.qt.io/qt-5/qobject.html#QObject does, which your timeWorker() does not.

                      Thoughts for you, only trying to be helpful....

                      1 Reply Last reply
                      3
                      • V Offline
                        V Offline
                        VRonin
                        wrote on 11 Jun 2020, 10:20 last edited by
                        #10

                        Unless you have a definition somewhere else (usually in the .cpp file) a constructur declaration is not enough. This is not related to Qt, this is pure C++ and it has been this way since the language existed

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        E 1 Reply Last reply 11 Jun 2020, 11:16
                        2
                        • V VRonin
                          11 Jun 2020, 10:20

                          Unless you have a definition somewhere else (usually in the .cpp file) a constructur declaration is not enough. This is not related to Qt, this is pure C++ and it has been this way since the language existed

                          E Offline
                          E Offline
                          Engelard
                          wrote on 11 Jun 2020, 11:16 last edited by
                          #11

                          @VRonin said in Why i cant create instance of the simpliest custom class?:

                          Unless you have a definition somewhere else (usually in the .cpp file) a constructur declaration is not enough. This is not related to Qt, this is pure C++ and it has been this way since the language existed

                          I ofc have the proper declaration and definition, as i said i have bunch of previous projects which made exactly that way, and those working.

                          Cutting a long story short, if you feel your code is correct and you are getting irritated by these questions, have you deleted all files from the debug/release compilation directory and re-run qmake on your project?

                          Irritated indeed, because i know the basics of C++ well, and i also know for 100% that i'm doing everything right but it get to me like something wrong, and it is - again Qt bug(or call that whatever you preffer but it is a bug).

                          I deleted debug-release folders, Rerun Qmake and now it SUDDENLY tell me that no errors and everything run fine!

                          J 1 Reply Last reply 11 Jun 2020, 11:20
                          0
                          • E Engelard
                            11 Jun 2020, 11:16

                            @VRonin said in Why i cant create instance of the simpliest custom class?:

                            Unless you have a definition somewhere else (usually in the .cpp file) a constructur declaration is not enough. This is not related to Qt, this is pure C++ and it has been this way since the language existed

                            I ofc have the proper declaration and definition, as i said i have bunch of previous projects which made exactly that way, and those working.

                            Cutting a long story short, if you feel your code is correct and you are getting irritated by these questions, have you deleted all files from the debug/release compilation directory and re-run qmake on your project?

                            Irritated indeed, because i know the basics of C++ well, and i also know for 100% that i'm doing everything right but it get to me like something wrong, and it is - again Qt bug(or call that whatever you preffer but it is a bug).

                            I deleted debug-release folders, Rerun Qmake and now it SUDDENLY tell me that no errors and everything run fine!

                            J Offline
                            J Offline
                            JonB
                            wrote on 11 Jun 2020, 11:20 last edited by JonB 6 Nov 2020, 11:23
                            #12

                            @Engelard
                            That's why I suggested the clean-out-and-re-make! It's not a Qt bug, but it is a problem with Qt Creator/qmake.

                            It is just life/how it is that when you find inexplicable compilation/link errors it may be that cached previous compilation stuff is the culprit. I don't like it any more than you do, but I often find I have to do this to get it to sort itself out, I agree it should be more robust at noticing changes.... I happen to know, I believe, that for example adding Q_OBJECT to an existing .h file seems to require clean-and-remake.

                            E 1 Reply Last reply 11 Jun 2020, 12:29
                            0
                            • J JonB
                              11 Jun 2020, 11:20

                              @Engelard
                              That's why I suggested the clean-out-and-re-make! It's not a Qt bug, but it is a problem with Qt Creator/qmake.

                              It is just life/how it is that when you find inexplicable compilation/link errors it may be that cached previous compilation stuff is the culprit. I don't like it any more than you do, but I often find I have to do this to get it to sort itself out, I agree it should be more robust at noticing changes.... I happen to know, I believe, that for example adding Q_OBJECT to an existing .h file seems to require clean-and-remake.

                              E Offline
                              E Offline
                              Engelard
                              wrote on 11 Jun 2020, 12:29 last edited by
                              #13

                              @JonB Tnx for reminding me about that delete/rebuild way.

                              1 Reply Last reply
                              0

                              1/13

                              10 Jun 2020, 23:15

                              • Login

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