I want to display on my GUI using QML and C++ a multi-datatype value of type template < typename T>. Is it possible to use it with Q_PROPERTY or shall I use function overloading for each data type and pass it to Q_PROPERTY ?
For example:
Q_PROPERTY(int dataread READ dataRead NOTIFY dataChanged)
Q_PROPERTY(float dataread READ dataRead NOTIFY dataChanged)
int dataRead (int data) {return data; }
float dataRead (float data) {return data; }
No, you can't use templates with properties, as moc doesnt support templating. This has been discussed (and is doable), but there hasn't been enough interest to implement it.
Each property also identifies a concrete type, so you can't overload it, either. You can overload signals and slots, so this is doable:
class foo : public QObject
{
Q_OBJECT
Q_PROPERTY(int intRead READ readInt NOTIFY dataChanged)
Q_PROPERTY(float floatRead READ readFloat NOTIFY dataChanged)
public:
int readInt() {return data; }
float readFloat() {return data; }
signals:
void dataChanged(int data);
void dataChanged(float data);
...
}
Note that I removed the parameters from your read functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With