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. Function parameters QML/C++

Function parameters QML/C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 3.5k 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.
  • A Offline
    A Offline
    alecs26
    wrote on 16 Feb 2017, 01:22 last edited by
    #1

    Hello,

    In order to communicate between my QML UI and my C++ code, I use ContextProperty.
    For instance, in my main.cpp, I have my main function:

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QQmlApplicationEngine engine;
    
        QQmlComponent componentmain(&engine,QUrl(QStringLiteral("qrc:/main.qml")));
        otherfunctions.objectmain=componentmain.create();
    
        QQmlContext* ctx = engine.rootContext();
        OtherFunctions otherfunctions;
        ctx->setContextProperty("otherfunctions", &otherfunctions);
    
        testmainfunc(otherfunctions); // explained later
    
        return app.exec();
    }
    

    From my QML code, I can access the functions from my class "otherfunctions". I can also access this class from my main function which is usefull to communicated between QML and C++.

    However, in my main.cpp, I have another function that I call from the main function: testmainfunc(otherfunctions). As a parameter, I want to send my class "otherfunctions" so this function can modify parameters from "otherfunctions" and thus communicate with the QML code.

    However, when I declare my function as "void testmainfunc(OtherFunctions otherfunctions);", I get the error :
    "error: use of deleted function 'OtherFunctions::OtherFunctions(const OtherFunctions&)'"

    If I declare my function as "void testmainfunc(OtherFunctions &otherfunctions);" [notice the &] it seems fine. For instance the following seems to work:

    void testmainfunc(OtherFunctions &otherfunctions)
    {
        otherfunctions.pathweb1insession="test1";
    }
    

    My question is

    1. Why "void testmainfunc(OtherFunctions otherfunctions);" does not work ?
    2. Is it OK to do as I did with "void testmainfunc(OtherFunctions &otherfunctions);" [notice the &] ?

    Thank you very much for your help !

    Alex

    J 1 Reply Last reply 16 Feb 2017, 05:46
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 16 Feb 2017, 01:53 last edited by
      #2

      Your object is Qobject. You r passing the object by value. Copy constructor is disabled in qt. Hence you hit the error.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      9
      • A alecs26
        16 Feb 2017, 01:22

        Hello,

        In order to communicate between my QML UI and my C++ code, I use ContextProperty.
        For instance, in my main.cpp, I have my main function:

        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
            QQmlApplicationEngine engine;
        
            QQmlComponent componentmain(&engine,QUrl(QStringLiteral("qrc:/main.qml")));
            otherfunctions.objectmain=componentmain.create();
        
            QQmlContext* ctx = engine.rootContext();
            OtherFunctions otherfunctions;
            ctx->setContextProperty("otherfunctions", &otherfunctions);
        
            testmainfunc(otherfunctions); // explained later
        
            return app.exec();
        }
        

        From my QML code, I can access the functions from my class "otherfunctions". I can also access this class from my main function which is usefull to communicated between QML and C++.

        However, in my main.cpp, I have another function that I call from the main function: testmainfunc(otherfunctions). As a parameter, I want to send my class "otherfunctions" so this function can modify parameters from "otherfunctions" and thus communicate with the QML code.

        However, when I declare my function as "void testmainfunc(OtherFunctions otherfunctions);", I get the error :
        "error: use of deleted function 'OtherFunctions::OtherFunctions(const OtherFunctions&)'"

        If I declare my function as "void testmainfunc(OtherFunctions &otherfunctions);" [notice the &] it seems fine. For instance the following seems to work:

        void testmainfunc(OtherFunctions &otherfunctions)
        {
            otherfunctions.pathweb1insession="test1";
        }
        

        My question is

        1. Why "void testmainfunc(OtherFunctions otherfunctions);" does not work ?
        2. Is it OK to do as I did with "void testmainfunc(OtherFunctions &otherfunctions);" [notice the &] ?

        Thank you very much for your help !

        Alex

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 16 Feb 2017, 05:46 last edited by
        #3

        @alecs26 To add to @dheerendra : if you want a function which modifies its parameter then you should pass this parameter as reference (&) or pointer (*), else your function will modify a copy! Read about passing parameters "by value", "by reference" and pointer in C++.

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alecs26
          wrote on 16 Feb 2017, 13:09 last edited by
          #4

          @jsulm
          Thank you for you answer.

          So is it OK to do that:
          in the main call: testmainfunc(otherfunctions);

          and the function:
          void testmainfunc(OtherFunctions &otherfunctions)
          {
          otherfunctions.pathweb1insession="test1";
          }

          Thank you !

          Alex

          J 1 Reply Last reply 16 Feb 2017, 13:20
          0
          • A alecs26
            16 Feb 2017, 13:09

            @jsulm
            Thank you for you answer.

            So is it OK to do that:
            in the main call: testmainfunc(otherfunctions);

            and the function:
            void testmainfunc(OtherFunctions &otherfunctions)
            {
            otherfunctions.pathweb1insession="test1";
            }

            Thank you !

            Alex

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 16 Feb 2017, 13:20 last edited by
            #5

            @alecs26 Actually you can answer your own question by just trying it out.
            But, yes passing by reference will work.

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

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 16 Feb 2017, 13:20 last edited by
              #6

              Yes it is ok as you r passing the object by reference.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              4
              • A Offline
                A Offline
                alecs26
                wrote on 16 Feb 2017, 19:54 last edited by
                #7

                @jsulm @dheerendra
                Thank you for your answer.

                @jsulm Yes I tried it and it worked but I wanted to be sure it was a proper way to do it.

                Thank you both very much !

                Alex

                1 Reply Last reply
                1

                1/7

                16 Feb 2017, 01:22

                • Login

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