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. QML can't call static member function of C++ class
Forum Updated to NodeBB v4.3 + New Features

QML can't call static member function of C++ class

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 4 Posters 1.5k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    Jazzy
    wrote on last edited by Jazzy
    #1

    C++ header file:

    class Worker: public Object
    {
    Q_OBJECT
    public:
         explicit Worker(QObject *parent = nullptr);
        Q_INVOKABLE static Worker fromJsonString(const QString &jsonString);
    }
    

    register Worker class type in my main.cpp:

    qmlRegisterType<Worker>("com.abc", 1, 0, "Worker");
    

    qml file:

    import com.abc 1.0
    var worker = Worker. fromJsonString(jsonString) 
    

    when I run the program, output following error message:

    TypeError: Property 'fromJsonString' of object [object Object] is not a function
    

    what wrong with my code?

    KroMignonK 1 Reply Last reply
    0
    • J Jazzy

      C++ header file:

      class Worker: public Object
      {
      Q_OBJECT
      public:
           explicit Worker(QObject *parent = nullptr);
          Q_INVOKABLE static Worker fromJsonString(const QString &jsonString);
      }
      

      register Worker class type in my main.cpp:

      qmlRegisterType<Worker>("com.abc", 1, 0, "Worker");
      

      qml file:

      import com.abc 1.0
      var worker = Worker. fromJsonString(jsonString) 
      

      when I run the program, output following error message:

      TypeError: Property 'fromJsonString' of object [object Object] is not a function
      

      what wrong with my code?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @Jazzy You miss to add Q_OBJECT and to declare fromJsonString() as public::

      class Worker: public Object
      {
          Q_OBJECT
      
      public:
           explicit Worker(QObject *parent = nullptr);
          Q_INVOKABLE static Worker fromJsonString(const QString &jsonString);
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      small_birdS 1 Reply Last reply
      1
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        You need an instance of an object to call a function in QML, so a static function doesn't make a lot of sense in QML.

        In your case you might want a singleton type acting as a Worker factory.

        1 Reply Last reply
        3
        • KroMignonK KroMignon

          @Jazzy You miss to add Q_OBJECT and to declare fromJsonString() as public::

          class Worker: public Object
          {
              Q_OBJECT
          
          public:
               explicit Worker(QObject *parent = nullptr);
              Q_INVOKABLE static Worker fromJsonString(const QString &jsonString);
          }
          
          small_birdS Offline
          small_birdS Offline
          small_bird
          wrote on last edited by
          #4

          @KroMignon whether I am wrong, it is said that your cold in the same with his, both have symbol Q_OBJECT.

          1 Reply Last reply
          0

          • Login

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