Bug in Qt Creator class generator?
-
wrote on 2 Mar 2022, 10:39 last edited by
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 itMyClass
and selectQObject
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 beQObject(parent)
? -
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 itMyClass
and selectQObject
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 beQObject(parent)
?@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
-
@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
-
@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 :@@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.
-
@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 :@@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.
-
wrote on 2 Mar 2022, 13:39 last edited by
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 :)
-
@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
wrote on 2 Mar 2022, 17:36 last edited byOK. 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/7