Q_DECLARE_METATYPE for template class
-
bq. This gives me a different error “value_type is not an element of Container<T>” in this line of qmetatype.h:
template<typename T, bool = QMetaTypeId2<typename T::value_type>::Defined>this line does not exist here! :) I use qt 5.1.1 mingw
-
I used an enum class as concrete type and I am using Qt5.2.0 beta here.
How did you involve QVariant?This is a minimum example that breaks compilation when used in one of projects headers:
@
template <class T> class Container
{
};Q_DECLARE_METATYPE_TEMPLATE_1ARG(Container)
inline void Test()
{
QVariant x = QVariant::fromValue(Container<int>());
}
@ -
it gives no error here ...
@Container<int> y = x.value< Container<int> >();@
returns the correctly too
shall I understand that Q_DECLARE_METATYPE_TEMPLATE_1ARG is working now?
-
No, it does NOT work. I also tried the code in one if the Qt examples to make sure there are no collisions in my custom project.
Adding the following code:
@
#include <QVariant>template <class T> class Container
{
};Q_DECLARE_METATYPE_TEMPLATE_1ARG(Container)
inline void Test()
{
QVariant x = QVariant::fromValue(Container<int>());
}@
to the bottom of the tabdialog.h header of TabWidget example returns errors on building:
@
C:\Qt\Qt5.2.0\5.2.0-beta1\msvc2012\include\QtCore\qmetatype.h:1372: Fehler:C2039: 'value_type': Ist kein Element von 'Container<T>'
with
[
T=int
]
C:\Qt\Qt5.2.0\5.2.0-beta1\msvc2012\include\QtCore/qmetatype.h(1578): Siehe Verweis auf die Instanziierung der gerade kompilierten Klassen-template "QtPrivate::SequentialContainerConverterHelper<T>".
with
[
T=Container<int>
]..... and a lot more
@
If it works for you I suppose this is a Qt 5.2.0 beta problem. I am using 5.2.0 beta, VS2012, ANGLE, 32 Bit.
-
I did this test with Qt5.1.1 (VS2012, ANGLE, 32 Bit) and it worked.
I also did it with Qt5.2.0-beta1 and it failed.
So I can definitely confirm that there is some strange issue with Qt5.2.0-beta1 (VS2012, ANGLE, 32 Bit).
-
Hi,
It sounds like a regression (I may be wrong though), there has been a lot of work on QMetatype lately. Can you open a bug report so it might get fixed before 5.2 official release ?
-
bq. I did this test with Qt5.1.1 (VS2012, ANGLE, 32 Bit) and it worked.
I also did it with Qt5.2.0-beta1 and it failed.
So I can definitely confirm that there is some strange issue with Qt5.2.0-beta1 (VS2012, ANGLE, 32 Bit).I can confirm these tests too, but I want to do one more with Qt 5.2.0-beta1 with mingw
-
I found a solution for Qt 5.2.0-beta1:
@class ContainerBase: public QString /!!!inherits a core class which is compatible with the new meta type system!!!/
{};
template <class T> class Container: public ContainerBase
{
T data;};
Q_DECLARE_METATYPE_TEMPLATE_1ARG(Container)@
-
EDIT: I created an issue, see https://bugreports.qt-project.org/browse/QTBUG-34957
-
Update:
According to the report (see above), the Q_DECLARE_METATYPE_TEMPLATE_1ARG in Qt5 seem to work as expected.
Unfortunately they do not intend to support registering template types (using a new public macro). So I will need to stick to my nasty workaround :-(
Anyway thanks for your help!