Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Bug in Qt Creator class generator?
Forum Updated to NodeBB v4.3 + New Features

Bug in Qt Creator class generator?

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
7 Posts 4 Posters 535 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.
  • P Offline
    P Offline
    paulmasri
    wrote on 2 Mar 2022, 10:39 last edited by
    #1

    I've noticed something strange in Qt Creator 6.0.2. It looks like a bug to me, but is it intentional? And if so, why?

    In a project, Add New...
    Select C++ Class
    Call it MyClass and select QObject as its base class.
    Leave other options as they appear and click Continue.

    The generated .cpp file looks like this:

    #include "myclass.h"
    
    MyClass::MyClass(QObject *parent)
        : QObject{parent}
    {
    
    }
    

    Why is the call to base class QObject{parent}? Shouldn't it be QObject(parent)?

    J 1 Reply Last reply 2 Mar 2022, 11:47
    0
    • P paulmasri
      2 Mar 2022, 10:39

      I've noticed something strange in Qt Creator 6.0.2. It looks like a bug to me, but is it intentional? And if so, why?

      In a project, Add New...
      Select C++ Class
      Call it MyClass and select QObject as its base class.
      Leave other options as they appear and click Continue.

      The generated .cpp file looks like this:

      #include "myclass.h"
      
      MyClass::MyClass(QObject *parent)
          : QObject{parent}
      {
      
      }
      

      Why is the call to base class QObject{parent}? Shouldn't it be QObject(parent)?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 2 Mar 2022, 11:47 last edited by
      #2

      @paulmasri said in Bug in Qt Creator class generator?:

      Shouldn't it be QObject(parent)?

      It's the same (C++11 invented {} initialisation syntax). See https://en.cppreference.com/w/cpp/language/constructor

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J P 2 Replies Last reply 2 Mar 2022, 13:25
      2
      • J jsulm
        2 Mar 2022, 11:47

        @paulmasri said in Bug in Qt Creator class generator?:

        Shouldn't it be QObject(parent)?

        It's the same (C++11 invented {} initialisation syntax). See https://en.cppreference.com/w/cpp/language/constructor

        J Offline
        J Offline
        JonB
        wrote on 2 Mar 2022, 13:25 last edited by
        #3

        @jsulm
        So apparently Qt6 Creator has decided to generate : QObject{parent} instead of the : QObject(parent) it has always used, and is used in most code one will see for methods in C++? Sorry, hate it! Grrr :@

        J J 2 Replies Last reply 2 Mar 2022, 13:29
        0
        • J JonB
          2 Mar 2022, 13:25

          @jsulm
          So apparently Qt6 Creator has decided to generate : QObject{parent} instead of the : QObject(parent) it has always used, and is used in most code one will see for methods in C++? Sorry, hate it! Grrr :@

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 2 Mar 2022, 13:29 last edited by
          #4

          @JonB said in Bug in Qt Creator class generator?:

          Sorry, hate it!

          Why? This change is only a problem if you're using an ancient compiler not capable of C++11.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • J JonB
            2 Mar 2022, 13:25

            @jsulm
            So apparently Qt6 Creator has decided to generate : QObject{parent} instead of the : QObject(parent) it has always used, and is used in most code one will see for methods in C++? Sorry, hate it! Grrr :@

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 2 Mar 2022, 13:33 last edited by
            #5

            @JonB its actually the method you're supposed to use for all initialisations, at least according to the committee members ;)

            apparently the compiler vendors can do more magic with that, something to do with constructors and functions..... I'm not sure, its beyond me.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JonB
              wrote on 2 Mar 2022, 13:39 last edited by
              #6

              My comments are only as I wrote them: my opinion only. Looks nasty, is different from the vast majority of C++ code out there. I didn't say it was wrong, just not nice :)

              I had a quick glance at the Qt6 documentation, and I do not see in the millions of lines of docs, method definitions, examples, etc. any change to make them all use {} where they have always had ()s. So now we will have noticeable differences between what people see around the web and what they get from Creator. And we will have questions like this one from people who are surprised.

              Like I said, only an IMHO. Thankfully I hope to keep clear of Qt6 for as long as possible, perhaps as long as people who still use/ask about/quote examples from Qt4 compared to Qt5 :)

              1 Reply Last reply
              0
              • J jsulm
                2 Mar 2022, 11:47

                @paulmasri said in Bug in Qt Creator class generator?:

                Shouldn't it be QObject(parent)?

                It's the same (C++11 invented {} initialisation syntax). See https://en.cppreference.com/w/cpp/language/constructor

                P Offline
                P Offline
                paulmasri
                wrote on 2 Mar 2022, 17:36 last edited by
                #7

                OK. Not a bug, but a bit of C++ I didn't know about.

                Looking at this thread and now having seen this question on StackOverflow, the clearest things I can say is that opinion is heavily divided as to whether brace-list initialisation is preferable.

                Personally I don't see the benefit and I do see a risk of me getting confused, so I'll be sticking with the rounded brackets. But good to know that these exist and are valid.

                @jsulm said in Bug in Qt Creator class generator?:

                It's the same (C++11 invented {} initialisation syntax). See https://en.cppreference.com/w/cpp/language/constructor

                To clarify for any other readers, brace-list initialisation isn't the same as rounded-bracket initialisation. (See the StackOverflow question above for examples.) However the behaviour is the same in this particular case.

                1 Reply Last reply
                0

                1/7

                2 Mar 2022, 10:39

                • Login

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