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. Is omitting a necessary Q_OBJECT always detected at build time?
Forum Updated to NodeBB v4.3 + New Features

Is omitting a necessary Q_OBJECT always detected at build time?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 509 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.
  • D Offline
    D Offline
    dave2
    wrote on last edited by
    #1

    Suppose I have a class which inherits from QObject, that this class uses modern slots (i.e., slots connected with the syntax introduced in Qt 5, not with macros SIGNAL() and SLOT()), and that this class does not use any of Qt's meta-object features. Then, this class doesn't need to be declared with Q_OBJECT.

    If I declare and emit a signal in that class, the build will fail if I forget to add Q_OBJECT to the declaration of the class.

    Are there any situations where omitting a necessary Q_OBJECT would not be detected at build time (i.e., by the compiler or by the linker), but at run-time instead?

    Pl45m4P 1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #7

      The Meta-Object System documentation provides a list of features:

      • QObject::metaobject()
      • QMetaObject::className()
      • QObject::inherits()
      • QObject::tr()
      • QObject::setProperty() and getProperty()
      • QMetaObject::newInstance()
      • qobject_cast<>()

      Without moc's output, these will behave as if called for the parent class.

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

      D 1 Reply Last reply
      1
      • D dave2

        Suppose I have a class which inherits from QObject, that this class uses modern slots (i.e., slots connected with the syntax introduced in Qt 5, not with macros SIGNAL() and SLOT()), and that this class does not use any of Qt's meta-object features. Then, this class doesn't need to be declared with Q_OBJECT.

        If I declare and emit a signal in that class, the build will fail if I forget to add Q_OBJECT to the declaration of the class.

        Are there any situations where omitting a necessary Q_OBJECT would not be detected at build time (i.e., by the compiler or by the linker), but at run-time instead?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #2

        @dave2 said in Is omitting a necessary Q_OBJECT always detected at build time?:

        Are there any situations where omitting a necessary Q_OBJECT would not be detected at build time (i.e., by the compiler or by the linker), but at run-time instead?

        Would you like this to happen or why are you asking?


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        D 1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @dave2 said in Is omitting a necessary Q_OBJECT always detected at build time?:

          Are there any situations where omitting a necessary Q_OBJECT would not be detected at build time (i.e., by the compiler or by the linker), but at run-time instead?

          Would you like this to happen or why are you asking?

          D Offline
          D Offline
          dave2
          wrote on last edited by
          #3

          @Pl45m4 I would very much want erroneous omissions of Q_OBJECT to be detected by the build system rather than at run-time. Being confident that any erroneous omission of Q_OBJECT is detected by the build system would help me remove unnecessary uses of that macro.

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            What's wrong with simply adding Q_OBJECT instead thinking about when it's maybe not needed (and you forget it by accident later on due to other, unrelated changes)?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            D 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              What's wrong with simply adding Q_OBJECT instead thinking about when it's maybe not needed (and you forget it by accident later on due to other, unrelated changes)?

              D Offline
              D Offline
              dave2
              wrote on last edited by
              #5

              @Christian-Ehrlicher Our project contains about two thousand modules (= about one thousand h files + one thousand cpp files). About half of those modules define classes which inherit, directly or indirectly, QObject. They currently all use Q_OBJECT. Most of them (perhaps eight hundred), however, do not define signals, and thus presumably would not need Q_OBJECT. Removing eight hundred unnecessary instances of Q_OBJECT would mean that eight hundred mocs would not need to be generated, compiled, and linked.

              Can you give one example where omitting a necessary Q_OBJECT would not be detected at build time, if such an example exists?

              D 1 Reply Last reply
              1
              • D dave2

                @Christian-Ehrlicher Our project contains about two thousand modules (= about one thousand h files + one thousand cpp files). About half of those modules define classes which inherit, directly or indirectly, QObject. They currently all use Q_OBJECT. Most of them (perhaps eight hundred), however, do not define signals, and thus presumably would not need Q_OBJECT. Removing eight hundred unnecessary instances of Q_OBJECT would mean that eight hundred mocs would not need to be generated, compiled, and linked.

                Can you give one example where omitting a necessary Q_OBJECT would not be detected at build time, if such an example exists?

                D Offline
                D Offline
                dave2
                wrote on last edited by
                #6

                @dave2 (As an aside: when I wrote my previous message with numbers (2000, 1000, 800), my reply was flagged as spam!)

                1 Reply Last reply
                0
                • jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #7

                  The Meta-Object System documentation provides a list of features:

                  • QObject::metaobject()
                  • QMetaObject::className()
                  • QObject::inherits()
                  • QObject::tr()
                  • QObject::setProperty() and getProperty()
                  • QMetaObject::newInstance()
                  • qobject_cast<>()

                  Without moc's output, these will behave as if called for the parent class.

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

                  D 1 Reply Last reply
                  1
                  • jeremy_kJ jeremy_k

                    The Meta-Object System documentation provides a list of features:

                    • QObject::metaobject()
                    • QMetaObject::className()
                    • QObject::inherits()
                    • QObject::tr()
                    • QObject::setProperty() and getProperty()
                    • QMetaObject::newInstance()
                    • qobject_cast<>()

                    Without moc's output, these will behave as if called for the parent class.

                    D Offline
                    D Offline
                    dave2
                    wrote on last edited by
                    #8

                    @jeremy_k You are right, thanks!

                    So, for example, you have a class, MyClass, which inherits QObject. If MyClass is declared with Q_OBJECT, MyClass::staticMetaObject.className() evaluates to "MyClass", whereas if MyClass is declared without Q_OBJECT, the same expression evaluates to "QObject". Both versions build without error, but the one without Q_OBJECT does not work as intended.

                    Of the listed features, however, the only ones we use are QObject::setProperty() and QObject::property(), but only in a very few places, and not on classes of our own (i.e., on classes of the Qt library). Therefore, we should be safe as far as these features are concerned.

                    Please let us know if you can think of other examples.

                    1 Reply Last reply
                    0
                    • D dave2 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