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 Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 4 Posters 1.4k 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.
  • J Offline
    J Offline
    Jazzy
    wrote on 22 Nov 2019, 03:57 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?

    K 1 Reply Last reply 22 Nov 2019, 06:47
    0
    • J Jazzy
      22 Nov 2019, 03:57

      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?

      K Offline
      K Offline
      KroMignon
      wrote on 22 Nov 2019, 06:47 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)

      S 1 Reply Last reply 23 Nov 2019, 03:34
      1
      • G Offline
        G Offline
        GrecKo
        Qt Champions 2018
        wrote on 22 Nov 2019, 09:32 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
        • K KroMignon
          22 Nov 2019, 06:47

          @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);
          }
          
          S Offline
          S Offline
          small_bird
          wrote on 23 Nov 2019, 03:34 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

          1/4

          22 Nov 2019, 03:57

          • Login

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