How to build real template class?
-
Using Qt to build C++ class I end up with this (fake) "template class" declaration.
That is what Qt dementation calls it -template class .Is there a way I can build REAL template class using Qt?
ADDENDUM
It is KISS - just add template <typename T> as type, BUT
then Q_OBJECT is "not supported" !which brings up another question
if ALL Qt "classes" inherit from QObject anywayso what does placing Q_OBJECT (insert its name here )
do anyway?Looks as I poked stick into hornets nest - all kinds of "stuff" I took for granted so far.
// cut an paste hack template <typename T> class TEST_Template_Hack : public QObject { Q_OBJECT public: explicit TEST_Template_Hack(QObject *parent = nullptr); signals: };
#ifndef TEST_TEMPLATE_H #define TEST_TEMPLATE_H #include <QObject> class TEST_Template : public QObject { Q_OBJECT public: explicit TEST_Template(QObject *parent = nullptr); signals: }; #endif // TEST_TEMPLATE_H
-
@AnneRanch
A template class is correct if it compiles, and wrong if it doesn't.
A class is either a template class, or it's not.
I don't understand what you mean by "fake" and "real".The only limitation in Qt is that you can't use Q_OBJECT in a template class.
But the template class can inherit from a class, that does have the Q_OBJECT macro.#include <QObject> class TEST : public QObject { Q_OBJECT public: explicit TEST_Template(QObject *parent = nullptr); signals: void aSignal(int number); }; template <typename T> class TEST_Template : public TEST { ... };
Looks as I poked stick into hornets nest - all kinds of "stuff" I took for granted so far.
Don't worry, no hornets in this neighbourhood.
-
The important question is: What are you trying to achieve?
I struggle to see a compelling use case for a templated QObject, but I might be missing something... -
I do not agree with your opinion : What are you trying to achieve?
My missed point is - Qt documentation call the result of their process of building a class a " template". It is NOT template TYPE by definition, it is Qt miss use of a term.
I have no illusion that pointing that out will "achieve " anything. Qt is pushing its own terminology and continue to do so irregardless of me , a user , opinion. They are the experts , in their opinions / view.
My only "achievement' could be to make others users of Qt aware. That is about all I could hope for. -
It is creating a template for a class derived from QObject, not a template class. You are mixing different stuff.
-
Could you link the relevant documentation page?
-
-
"Template" in this context is simply an English word in the same way as you may have a "Template for a presentation", i.e. something generic to help you get started, from which you can then customize what you want.
-
I disagree , especially when it is " for beginners" .
Cheers -
It is NOT template TYPE by definition, it is Qt miss use of a term.
That's simply wrong, @AnneRanch.
The misunderstanding is entirely on your side.As @Asperamanca indicated, the term "template" is standard English vocabulary. If you reserve it exclusively for what is a template in C++, you limit your own vocabulary.
The documentation page you referred, is truly outdated, but it doesn't contain anything wrong. It uses word "template" in two different contexts:
- TEMPLATE directive of qmake
- creating a class definition, following a template (as @Asperamanca already explained)
The page does not contain any mention of the term "template" in C++ code. That makes the context pretty obvious for any C++ programmer, beginners included.
-
@AnneRanch said in How to build real template class?:
I disagree , especially when it is " for beginners" .
CheersI agree that the conflict of terms here is a bit unfortunate. Then again, I cannot come up with a more fitting term for "template" in this context. If you have a better idea, you can also write documentation suggestion to bugreports.qt.io
-
@Asperamanca said in How to build real template class?:
I cannot come up with a more fitting term for "template" in this context
I can think of another term (don't know if better or not), but I'm not allowed to comment :)))
The first part is a color, the second part of the word is what a certain I/O device does :))) -
@Redman
C'mon, ever seen a yellow fax?
He meant orange jam, obviously. -
@Axel-Spoerl said in How to build real template class?:
He meant orange jam, obviously.
Actually I was referring to commonly used term "reddisplay".
... but I agreed to @Redman so he wouldn't feel bad :))) -
bluescreen :-)
-
I got it: Black Label!
Edit: Green Card. -
This took me exceedingly long to get omfg.