Results 1 to 16 of 16

Thread: qml can not recognize the c++ parameters with &

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default qml can not recognize the c++ parameters with &

    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
    Qt Code:
    1. #ifndef TESTPARAM_HPP
    2. #define TESTPARAM_HPP
    3.  
    4. #include <QList>
    5. #include <QObject>
    6. #include <QUrl>
    7.  
    8. class testParam : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit testParam(QObject *parent = 0);
    13.  
    14. signals:
    15.  
    16. public slots:
    17. void test(QList<QUrl> &first, QList<QUrl> &second)
    18. {
    19.  
    20. }
    21.  
    22. };
    23.  
    24. #endif // TESTPARAM_HPP
    To copy to clipboard, switch view to plain text mode 


    .cpp
    Qt Code:
    1. #include "testParam.hpp"
    2.  
    3. testParam::testParam(QObject *parent) :
    4. QObject(parent)
    5. {
    6. }
    To copy to clipboard, switch view to plain text mode 

    main.qml

    Qt Code:
    1. import QtQuick 2.1
    2. import QtQuick.Dialogs 1.0
    3.  
    4. import Test 1.0
    5.  
    6. Rectangle {
    7. width: 100
    8. height: 62
    9.  
    10. TestParam{
    11. id: testParam
    12. }
    13.  
    14. FileDialog{
    15. id: fileDialog
    16. //selectFolder: param.selectFolder
    17. selectMultiple: true
    18. nameFilters: [ "Image files (*.bmp *.jpg *.JPEG *.png *.ppm *.tiff *.xbm *.xpm)" ]
    19. onAccepted: {
    20. testParam.test(fileDialog.fileUrls, fileDialog.fileUrls);
    21. }
    22. }
    23.  
    24. MouseArea{
    25. anchors.fill: parent
    26.  
    27. onClicked: {
    28. fileDialog.open();
    29. }
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml can not recognize the c++ parameters with &

    QML has no notion of references. The data is always passed by value.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    stereoMatching (25th June 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qml can not recognize the c++ parameters with &

    Do you want to change the lists in the method?
    I.e. why not use const &?

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    stereoMatching (25th June 2013)

  6. #4
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qml can not recognize the c++ parameters with &

    Quote Originally Posted by anda_skoa View Post
    Do you want to change the lists in the method?
    I.e. why not use const &?

    Cheers,
    _
    I tried it, like this

    Qt Code:
    1. QList<QString> fileProcess::unique_strs(QList<QString> const &old_data, QList<QString> const &new_data)
    2. {
    3. //awkward solution and maybe meaningless, hope that in the future this could be solved
    4. return set_difference_lazy(const_cast<QList<QString>&>(new_data), const_cast<QList<QString>&>(old_data));
    5. }
    To copy to clipboard, switch view to plain text mode 

    There are two problems
    1 : this solution is awkward
    2 : I don't know the c++ sites would accept the parameters as reference or just copy them anyway?

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml can not recognize the c++ parameters with &

    You should first understand why QML does not like to accept references. If you have an object in QML, say.... "X" and you invoke your slot on it, it gets converted from the script representation to the C++ representation (to the "real" X type) and that gets passed to your slot. Which means that even if you modify that data in your slot, none of those modifications will be viisble from QML because the engine will not know to convert the "real" X type back to the script representation.

    Another problem is using references as arguments to slots that can be ran in a different thread to that where the signal emission took place. This is an asynchronous call so you have no idea when the original variable is going to be accessed (and possibly modified). Qt takes a copy of the original object and stores that safely until the real invokation takes place. So again, a copy of the original structure is modified and has no influence on the initial variable which remains unchanged (hence again discarding any sense of using a non-const reference).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    stereoMatching (25th June 2013)

  9. #6
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qml can not recognize the c++ parameters with &

    In the conclusion, we must copy the data between qml and c++?

    For me, no matter it may or may not an asynchronous call, we should
    have the power to determine the parameters are pass by reference or value
    It is the responsible of the programmers to handle the asynchronous issues
    Is this another technicle difficulties of qml so they can't support?

  10. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml can not recognize the c++ parameters with &

    First you should tell us why you want to pass those lists by non-const reference.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #8
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qml can not recognize the c++ parameters with &

    Quote Originally Posted by wysota View Post
    First you should tell us why you want to pass those lists by non-const reference.
    I am Using fileDialog to select a lot of files from qml(hundred, thousand, ten thousand and maybe more)
    and need to do some post processing about the file names obtained by the fileDialog(sort, find out new strings between old strings and new strings,
    prepend text, strip text and so on)

    Even this is not a performance penalty yet, it is a waste to copy a bunch of strings, if it is possible, I would like to pass by reference

    ps : pass by const reference is no use, I test it already, any alternation on the parameters pass into c++
    would not affect the data of qml

  12. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml can not recognize the c++ parameters with &

    Quote Originally Posted by stereoMatching View Post
    and need to do some post processing about the file names obtained by the fileDialog(sort, find out new strings between old strings and new strings,
    prepend text, strip text and so on)
    So return the modified list instead of changing the original one. You'll be making a copy either way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #10
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qml can not recognize the c++ parameters with &

    Quote Originally Posted by wysota View Post
    So return the modified list instead of changing the original one. You'll be making a copy either way.
    But before I can return the modified list, I need to get the data from qml site
    It would be great if the return data could be moved(rvalue reference, rvo) to the qml site

    What would you try to do if there are a lot of data need to transfer between c++ and qml?

    From the view of technical, is it possible to make c++ and qml support pass by reference?

  14. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml can not recognize the c++ parameters with &

    Quote Originally Posted by stereoMatching View Post
    But before I can return the modified list, I need to get the data from qml site
    It would be great if the return data could be moved(rvalue reference, rvo) to the qml site
    It can't because C++ and QML use different representation for the data.

    What would you try to do if there are a lot of data need to transfer between c++ and qml?
    I would probably not move betweeen C++ and QML at all.

    If you don't modify the lists then use const references but that will make a copy anyway.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qml can not recognize the c++ parameters with &

    It is likely that the fileUrls property of FileDialog is actually of type QStringList, so passing the value to the C++ function might not involve any conversions.

    In which case passing by const reference would not create a copy. Even passing by value would be cheap since QList is implicitly shared (reference counted) so copying is a relatively cheap operation.

    Do you have any benchmark that shows the operation to be slow?

    Cheers,
    _

  16. #13
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qml can not recognize the c++ parameters with &

    It is likely that the fileUrls property of FileDialog is actually of type QStringList
    I hope so, but it is QList<QUrl> but not QList<QString>
    in the document of the FileDialog, there are nothing called fileUrl but filePath
    and the qml type of filePath is list<string>
    maybe this is because I am using 5.1RC, so the incoherent of the codes and the document still exist

    Even passing by value would be cheap since QList is implicitly shared (reference counted)
    Hard to said, because I need to modify the content of QList(sorting)

    Do you have any benchmark that shows the operation to be slow?
    No, it is not a bottleneck yet(maybe in the future, it is)
    but the experience from c++(and c) make me feel nervous
    when I have to copy the data which could be "moved" or take the reference usually

  17. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qml can not recognize the c++ parameters with &

    Quote Originally Posted by stereoMatching View Post
    I hope so, but it is QList<QUrl> but not QList<QString>
    I see, should still be ok. Your slot has the same type for its arguments.

    Quote Originally Posted by stereoMatching View Post
    Hard to said, because I need to modify the content of QList(sorting)
    Depends on the reference count at the time of modification. If the slot is the only one holding the list at that time it won't need to copy.
    If there is another owner, then there will be a copy of the list. Since you don't change the url data the QUrl instances should still be the same.

    Quote Originally Posted by stereoMatching View Post
    No, it is not a bottleneck yet(maybe in the future, it is)
    but the experience from c++(and c) make me feel nervous
    when I have to copy the data which could be "moved" or take the reference usually
    Due to the implicit sharing, copying is a lot like moving, actual copying happening on modification (copy on write).

    Cheers,
    _

  18. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml can not recognize the c++ parameters with &

    Quote Originally Posted by anda_skoa View Post
    Depends on the reference count at the time of modification. If the slot is the only one holding the list at that time it won't need to copy.
    The reference count will not be 1 as the caller has a handle on the list as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #16
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qml can not recognize the c++ parameters with &

    I see, should still be ok. Your slot has the same type for its arguments.
    Because I want to make my question become easier to understand, I change the type of the example
    the real type are a little bit tricky, one is "QUrl" and another one is "QString"
    hope this could be fix in the release version

Similar Threads

  1. Qt5 don't recognize pri files !!?
    By alrawab in forum Newbie
    Replies: 4
    Last Post: 15th January 2013, 15:41
  2. Can't recognize the beforeInsert signal
    By scot_hansen in forum Newbie
    Replies: 0
    Last Post: 29th October 2010, 19:05
  3. How do I get Qt to recognize the oci driver.
    By yleesun in forum Qt Programming
    Replies: 11
    Last Post: 19th January 2009, 04:50
  4. How to recognize a letter from a Qstring?
    By luffy27 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2007, 20:22
  5. Getting Qt to recognize plugins...
    By KShots in forum Qt Programming
    Replies: 4
    Last Post: 21st April 2007, 11:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.