Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to register a class inherited from QQmlPropertyValueSource?
Forum Updated to NodeBB v4.3 + New Features

How to register a class inherited from QQmlPropertyValueSource?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 651 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Anton1978A Offline
    Anton1978A Offline
    Anton1978
    wrote on last edited by
    #1

    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_H
    

    realization

    #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 me
    

    What does he want??? How to register a C++ class so that you can already use it?

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2
      qmlRegisterType<RandomNumber>("RandomNumber",1,1,"RandomNumber"); //didn't work - reports that abstract type cannot be registered
      

      Make 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 specify virtual (it's useless) but add override at 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.

      1 Reply Last reply
      2
      • Anton1978A Offline
        Anton1978A Offline
        Anton1978
        wrote on last edited by Anton1978
        #3

        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 this

        class RandomNumber : public QObject, public QQmlPropertyValueSource
        {
            Q_OBJECT
            Q_PROPERTY(int maxValue READ maxValue WRITE setMaxValue NOTIFY maxValueChanged FINAL)
            Q_INTERFACES(QQmlPropertyValueSource)
            QML_ELEMENT
        

        and 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

        jsulmJ 1 Reply Last reply
        0
        • Anton1978A Anton1978

          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 this

          class RandomNumber : public QObject, public QQmlPropertyValueSource
          {
              Q_OBJECT
              Q_PROPERTY(int maxValue READ maxValue WRITE setMaxValue NOTIFY maxValueChanged FINAL)
              Q_INTERFACES(QQmlPropertyValueSource)
              QML_ELEMENT
          

          and 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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Anton1978A Offline
            Anton1978A Offline
            Anton1978
            wrote on last edited by Anton1978
            #5

            and nothing
            build error - :-1: ninja: build stopped: subcommand failed.
            wait, I didn't clean CMakeLists.txt

            jsulmJ 1 Reply Last reply
            0
            • Anton1978A Offline
              Anton1978A Offline
              Anton1978
              wrote on last edited by
              #6

              yes, now it works.
              Thank you very much

              1 Reply Last reply
              0
              • Anton1978A Anton1978

                and nothing
                build error - :-1: ninja: build stopped: subcommand failed.
                wait, I didn't clean CMakeLists.txt

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • Anton1978A Anton1978 has marked this topic as solved on

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved