Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved]qml can not recognize the c++ parameters with &

    QML and Qt Quick
    2
    11
    5921
    Loading More Posts
    • 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.
    • S
      stereomatching last edited by

      pass two arguments into the c++ api from qml site
      when I add & on the api, the app can not recognize the signature
      of the function.

      .hpp
      @
      #ifndef TESTPARAM_HPP
      #define TESTPARAM_HPP

      #include <QList>
      #include <QObject>
      #include <QUrl>

      class testParam : public QObject
      {
      Q_OBJECT
      public:
      explicit testParam(QObject *parent = 0);

      signals:

      public slots:
      void test(QList<QUrl> &first, QList<QUrl> &second)
      {

      }
      

      };

      #endif // TESTPARAM_HPP

      @

      .cpp
      @
      #include "testParam.hpp"

      testParam::testParam(QObject *parent) :
      QObject(parent)
      {
      }

      @

      main.qml
      @
      import QtQuick 2.1
      import QtQuick.Dialogs 1.0

      import Test 1.0

      Rectangle {
      width: 100
      height: 62

      TestParam{
          id: testParam
      }
      
      FileDialog{
          id: fileDialog
          //selectFolder: param.selectFolder
          selectMultiple: true
          nameFilters: [ "Image files (*.bmp *.jpg *.JPEG *.png *.ppm *.tiff *.xbm *.xpm)" ]
          onAccepted: {
              testParam.test(fileDialog.fileUrls, fileDialog.fileUrls);
          }
      }
      
      MouseArea{
          anchors.fill: parent
      
          onClicked: {
              fileDialog.open();
          }
      }
      

      }
      @

      error message

      Error: Unknown method parameter type: QList<QUrl>&

      They are fine if
      1 : strip the &
      2 : reduce the parameters of "test" to a single parameter, that way you can preserve the &

      Anyway to pass the parameters as reference into the "test" with two parameters?

      1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        Data will be copied anyway, as JavaScript lists and QList are different. It does seem a bit strange, though, especially the part about using only one parameter.

        Try usign QVariant, maybe? And then getting the data using QVariant::value<>().

        (Z(:^

        1 Reply Last reply Reply Quote 0
        • S
          stereomatching last edited by

          [quote author="sierdzio" date="1372142579"]
          Try usign QVariant, maybe? And then getting the data using QVariant::value<>().[/quote]

          QVariant can prevent the copy of the data?
          Could you show me an example?

          1 Reply Last reply Reply Quote 0
          • sierdzio
            sierdzio Moderators last edited by

            It might, although it probably won't. I was rather thinking about allowing you to use 2 parameters.

            (Z(:^

            1 Reply Last reply Reply Quote 0
            • S
              stereomatching last edited by

              Do we have a way to pass heavy data between qml and c++ without copy?

              1 Reply Last reply Reply Quote 0
              • sierdzio
                sierdzio Moderators last edited by

                By using QObject pointers.

                For me, const QString references and QLists of pointers also do work. I don't know the internals of the engine well enough to answer your question with confidence.

                (Z(:^

                1 Reply Last reply Reply Quote 0
                • S
                  stereomatching last edited by

                  [quote author="sierdzio" date="1372144978"]By using QObject pointers.

                  For me, const QString references and QLists of pointers also do work. I don't know the internals of the engine well enough to answer your question with confidence.[/quote]

                  Could you show me some example?

                  1 Reply Last reply Reply Quote 0
                  • sierdzio
                    sierdzio Moderators last edited by

                    • "link":https://github.com/sierdzio/closecombatfree/blob/master/src/ccfgamemanager.h
                    • "link":https://github.com/sierdzio/closecombatfree/blob/master/src/logic/ccfenginehelpers.h
                    • and "link":https://github.com/sierdzio/closecombatfree/blob/master/src/logic/ccfscenariostate.h

                    Feel free to check out the whole project, it can easily be browsed in Qt Creator.

                    (Z(:^

                    1 Reply Last reply Reply Quote 0
                    • S
                      stereomatching last edited by

                      Thanks for your help.

                      Since a lot of heavy jobs would be done on c++ sites, I hope in the future we could
                      pass the parameters between c++ and qml by reference.

                      1 Reply Last reply Reply Quote 0
                      • S
                        stereomatching last edited by

                        Can't find a reasonable way to transform between string and QObject
                        so I try const&

                        @
                        /**

                        • @brief find out the different string of new_data compare with the old data
                        • @param old_data : the old data
                        • @param new_data : the new data
                        • @return the combination of the old data and the different string of new_data compare with the old data
                        • @example old_data = {1, 2, 3, 4}, new_data = {1, 3, 4, 5}, results = {1, 2, 3, 4, 5}
                        • @caution : qml site could not recognize the parameters with &(even it can, it still copy the parameters rather than take the reference)
                        •        pass the parameters by const& seems work, but I am not sure it would not copy the data by this way
                          

                        */
                        QList<QString> fileProcess::unique_strs(QList<QString> const &old_data, QList<QString> const &new_data)
                        {
                        //awkward solution and maybe meaningless, hope that in the future this could be solved
                        return set_difference_lazy(const_cast<QList<QString>&>(new_data), const_cast<QList<QString>&>(old_data));
                        }
                        @

                        1 Reply Last reply Reply Quote 0
                        • S
                          stereomatching last edited by

                          After some experiment, the last post of my solution is useless, the data of
                          qml and c++ are always copy

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post