Solved C++ template member method callable from QML
-
Hi,
I need a function that can take an int,float,double or stringclass PlcCom : public QObject { Q_OBJECT public: explicit PlcCom(QObject *parent = nullptr); template<typename T> void setValue(T v){ QString input = typeid(v).name(); } }
but i can't call this method from QML..
If i mark this method as public slot i will get
Error: Template function as signal or slot
If i mark it Q_INVOKABKE i get :
moc_plccom.cpp:180: erreur : expected type-specifier before 'T'
case 18: _t->setValue((reinterpret_cast< T()>(_a[1]))); break;
moc_plccom.cpp:180: erreur : expected '>' before 'T'
moc_plccom.cpp:180: erreur : expected '>' before 'T'
moc_plccom.cpp:180: erreur : expected primary-expression before ')' token
case 18: _t->setValue((reinterpret_cast< T()>(_a[1]))); break;
moc_plccom.cpp:180: erreur : 'T' was not declared in this scope
case 18: _t->setValue((reinterpret_cast< T()>(_a[1]))); break;How to call a tamplate member function from QML please ?
-
@LeLev said in C++ template member method callable from QML:
How to call a tamplate member function from QML please ?
I do not think this is possible.
I think the best way is to used QVariant as parameter, because QML needs to be able to convert it own type into matching C++ type.Something like this:
class PlcCom : public QObject { Q_OBJECT public: explicit PlcCom(QObject *parent = nullptr); Q_INVOKABLE void setValue(const QVariant& v){ if(v.isValid()) { qDebug() << "Received" << v.typeName() << "value"; } else { qDebug() << "Invalid value!"; } } }
-
hi @KroMignon
thak you very much for the quick answer