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. Unknown method parameter type: QString&
Qt 6.11 is out! See what's new in the release blog

Unknown method parameter type: QString&

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 7.6k Views 2 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.
  • L Offline
    L Offline
    liubbc
    wrote on last edited by sierdzio
    #1

    Hi, Qters

    When QML call C++ class function, an error occur :
    Unknown method parameter type: QString&

    The code snippet are :

    //calculator.h 
    class Calculator : public QObject{
            public: 
            Q_INVOKABLE QString getResult(QString & leftValue, QString & rightValue);
    }
    
    //calculator.cpp
    QString Calculator::getResult(QString & leftValue, QString & rightValue){
        return "abc";
    }
    
    //qml
    
    Calculator{
            id: calculator;
        }
    
    display.text = calculator.getResult("abc", "bcd");
    

    if class Calculator getResult function is defined :

    class Calculator : public QObject{
            public: 
            Q_INVOKABLE QString getResult(QString  leftValue, QString  rightValue);
    }
    

    the error disappeared.

    but i do not know the root cause. Does anybody know the reason ? Thanks in advance !

    [edit: sierdzio. Added code tags]

    1 Reply Last reply
    1
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      QML connections are handled via MOC in Qt, which places strict rules on parameters - to prevent problems in threads. More info.

      When a function is called via Q_INVOKABLE (or it is a slot), parameter is always passed by value (copy ). Const references also work.

      (Z(:^

      L 1 Reply Last reply
      3
      • L Offline
        L Offline
        liubbc
        wrote on last edited by
        #3

        Many thanks!

        AlienA 1 Reply Last reply
        0
        • L liubbc

          Many thanks!

          AlienA Offline
          AlienA Offline
          Alien
          wrote on last edited by Alien
          #4

          Dear @liubbc,
          Could you please mark this topic to SOLVED
          Thanks a lot ;)

          L 1 Reply Last reply
          0
          • sierdzioS sierdzio

            QML connections are handled via MOC in Qt, which places strict rules on parameters - to prevent problems in threads. More info.

            When a function is called via Q_INVOKABLE (or it is a slot), parameter is always passed by value (copy ). Const references also work.

            L Offline
            L Offline
            liubbc
            wrote on last edited by
            #5

            @sierdzio Many Thanks

            1 Reply Last reply
            0
            • AlienA Alien

              Dear @liubbc,
              Could you please mark this topic to SOLVED
              Thanks a lot ;)

              L Offline
              L Offline
              liubbc
              wrote on last edited by
              #6

              @Alien So sorry, i try many times , but how to translate UNSOLVED to SOLVED ?

              AlienA 1 Reply Last reply
              0
              • L liubbc

                @Alien So sorry, i try many times , but how to translate UNSOLVED to SOLVED ?

                AlienA Offline
                AlienA Offline
                Alien
                wrote on last edited by
                #7

                @liubbc your welcome,
                click on "Topic Tools" button on right bottom of the page and then select the "Mark as solved"
                have a nice day

                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