How to register a class inherited from QQmlPropertyValueSource?
-
Good afternoon.
Version Qt6 6.5. I want to make the class visible in QML, it worked, but QML does not see the class field.
The class itself#ifndef RANDOMNUMBER_H #define RANDOMNUMBER_H #include <QObject> #include <QQmlProperty> #include <QQmlPropertyValueSource> #include <qqml.h> class QTimer; class RandomNumber : public QObject, public QQmlPropertyValueSource { Q_OBJECT Q_PROPERTY(int maxValue READ maxValue WRITE setMaxValue NOTIFY maxValueChanged FINAL) Q_INTERFACES(QQmlPropertyValueSource) // QML_ELEMENT // QML_VALUE_TYPE(RandomNumber) public: explicit RandomNumber(QObject *parent = nullptr); virtual void settarget(const QQmlProperty &pop); int maxValue() const; void setMaxValue(int newMaxValue); signals: void maxValueChanged(); public slots: void updatePropety(); private: QQmlProperty _targetPropety; QTimer *_timer = nullptr; int m_maxValue; }; #endif // RANDOMNUMBER_Hrealization
#include "randomnumber.h" #include <qtimer.h> #include <QObject> #include <QRandomGenerator64> RandomNumber::RandomNumber(QObject *parent) { m_maxValue = 100; _timer = new QTimer(); QObject::connect(_timer,&QTimer::timeout,this,&RandomNumber::updatePropety); _timer->start(1000); } void RandomNumber::settarget(const QQmlProperty &pop) { _targetPropety = pop; } int RandomNumber::maxValue() const { return m_maxValue; } void RandomNumber::updatePropety() { _targetPropety.write(QRandomGenerator64::global()->bounded(m_maxValue)); } void RandomNumber::setMaxValue(int newMaxValue) { if (m_maxValue == newMaxValue) return; m_maxValue = newMaxValue; emit maxValueChanged(); }and part of QML
import RandomNumber 1.0 //еще код Rectangle{ id: rectus color: "red" width: 150 height: 150 RandomNumber on radius //here is our class { maxValue : 50 // and our propetis } Text{ //text: moo.HuiNia(); text: wooda.word + func(); } }class is not registered, I tried like this
qmlRegisterType<RandomNumber>("RandomNumber",1,1,"RandomNumber"); //didn't work - reports that abstract type cannot be registered qmlRegisterType<RandomNumber>("RandomNumber",1,1,"RandomNumber","Cant create"); // it registers like this, but as expected it doesn’t work in QML qmlRegisterExtendedType<RandomNumber>("RandomNumber",1,1,"RandomNumber"); // does not register, the error message is not clear to meWhat does he want??? How to register a C++ class so that you can already use it?
-
qmlRegisterType<RandomNumber>("RandomNumber",1,1,"RandomNumber"); //didn't work - reports that abstract type cannot be registeredMake your class not abstract: override the pure virtual function
virtual void setTarget(const QQmlProperty &property) = 0.You declared
virtual void settarget(const QQmlProperty &pop);which is a typo.
When overriding a function don't specifyvirtual(it's useless) but addoverrideat the end to make sure you are actually overriding something.
The following would have gave you a compile error since it was marked as override but didn't override anything:void settarget(const QQmlProperty &pop) override;Also prefer QML_ELEMENT macros over
qmlRegisterType. -
It doesn't work.
void settarget(const QQmlProperty &pop) override;this doesn't work in principle, there's nothing to overload there. Why did I write so strangely? Because I’ve only been studying this for three days and was guided by one video, here
I think virtual is not really needed, but who knows. But there is version 5.11.
I also tried to do thisclass RandomNumber : public QObject, public QQmlPropertyValueSource { Q_OBJECT Q_PROPERTY(int maxValue READ maxValue WRITE setMaxValue NOTIFY maxValueChanged FINAL) Q_INTERFACES(QQmlPropertyValueSource) QML_ELEMENTand in the file, CMakeLists.txt
qt6_add_qml_module(RandomNumber URI randomNumber VERSION 1.0 )But this doesn’t work either, it compiles, but class is not visible in QML (if you use it, there will be an error).
I really don’t understand what is needed, everything worked in the video tutorial.
Just in case, here is my entire project -
It doesn't work.
void settarget(const QQmlProperty &pop) override;this doesn't work in principle, there's nothing to overload there. Why did I write so strangely? Because I’ve only been studying this for three days and was guided by one video, here
I think virtual is not really needed, but who knows. But there is version 5.11.
I also tried to do thisclass RandomNumber : public QObject, public QQmlPropertyValueSource { Q_OBJECT Q_PROPERTY(int maxValue READ maxValue WRITE setMaxValue NOTIFY maxValueChanged FINAL) Q_INTERFACES(QQmlPropertyValueSource) QML_ELEMENTand in the file, CMakeLists.txt
qt6_add_qml_module(RandomNumber URI randomNumber VERSION 1.0 )But this doesn’t work either, it compiles, but class is not visible in QML (if you use it, there will be an error).
I really don’t understand what is needed, everything worked in the video tutorial.
Just in case, here is my entire project@Anton1978 said in How to register a class inherited from QQmlPropertyValueSource?:
this doesn't work in principle
Of course not, because it is setTarget, not settarget as was pointed out by @GrecKo
-
and nothing
build error - :-1: ninja: build stopped: subcommand failed.
wait, I didn't clean CMakeLists.txt@Anton1978 said in How to register a class inherited from QQmlPropertyValueSource?:
build error - :-1: ninja: build stopped: subcommand failed.
Just a tipp: what you posted is not the actual error, in such cases you should look above that to find the real error message.
-
A Anton1978 has marked this topic as solved on