Results 1 to 4 of 4

Thread: QMl lost parameter when call a c++ function

  1. #1
    Join Date
    Aug 2013
    Posts
    27
    Thanks
    3

    Unhappy QMl lost parameter when call a c++ function

    There's a problem when I call C++ function in QML. I already upload souce code in attach file.
    In my project, i export a class to QML in main function.
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QGuiApplication app(argc, argv);
    4. qRegisterMetaType<ProgramParam>("ProgramParam");
    5. qRegisterMetaType<ProgramParamList>("ProgramParamList");
    6. qmlRegisterType<ProgramParam>("ProgramParam",1,0,"ProgramParam");
    7. qmlRegisterType<ConnectionBridge>("ConnectionBridge",1,0,"ConnectionBridge");
    8. QQmlApplicationEngine engine;
    9. engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    In ConnectionBridg class, I export a function to append param to paramList
    Qt Code:
    1. Q_INVOKABLE void appendParam(ProgramParam p);
    2.  
    3. void ConnectionBridge::appendParam(ProgramParam p)
    4. {
    5. qDebug() << "ConnectionBridge::appendParam p.size :" << p.m_addrList.size();
    6. m_programParamList.append(p);
    7. }
    8.  
    9. void ProgramParamList::append(ProgramParam &p)
    10. {
    11. qDebug() << "ProgramParamList::append p.size :" << p.m_addrList.size();
    12.  
    13. m_programParamList.append(p);
    14. }
    To copy to clipboard, switch view to plain text mode 

    And what I want to do is when "program " button clicked, QML can prepare several ProgramParam object, and append it to m_programParamList. You can find it in main.qml, line 755 to line 776.
    Qt Code:
    1. var programConfig = prepareOneTypeProgramConfig(i);
    2. if(!programConfig.isEmpty())
    3. {
    4. // console.log("add to myltyProgramParam!");
    5. // programConfig.print();
    6. var aProgramParam = Qt.createQmlObject('import ProgramParam 1.0; ProgramParam {}', buttonProgram, "dynamicSnippet1");
    7.  
    8. aProgramParam.addrList = programConfig.addrList;
    9. console.log("update addressList");
    10. aProgramParam.controlerType = programConfig.controlerType;
    11. aProgramParam.flashFilePath = programConfig.flashFilePath;
    12. aProgramParam.eepFilePath = programConfig.eepFilePath;
    13. aProgramParam.bUpdateEEP = programConfig.bUpdateEEP;
    14. aProgramParam.bUpdateFlash = programConfig.bUpdateFlash;
    15. aProgramParam.bUpdateFactorySetting = programConfig.bUpdateFactory;
    16. aProgramParam.factorySettingPath = programConfig.factoryFilePath;
    17. aProgramParam.bUpdateSN = programConfig.bUpdateSN;
    18. aProgramParam.firstSN = programConfig.sn;
    19. aProgramParam.bNeedSyncTime = programConfig.bNeedTimeSync;
    20. aProgramParam.bNeedResorteSetting = programConfig.bNeedRestoreCustomerSetting;
    21. console.log("before appendParam addressList");
    22. aConnectionBridge.appendParam(aProgramParam);
    To copy to clipboard, switch view to plain text mode 

    The problem is: when aConnectionBridge.appendParam(aProgramParam); called, QML create a new ProgramParam object, and pass the new ProgramParam object to ProgramParam's copy construct function. Thus, I get a empty ProgramParam list in m_programParamList.
    Log when button clicked as bellow:
    Qt Code:
    1. create a new ProgramParam ---------------------> creat a new object in QML
    2. set address! size is 2 ---------------------> assigne data, here address list size is 2
    3. qml: update addressList
    4. qml: before appendParam addressList ----------------------> before call aConnectionBridge.appendParam(aProgramParam);
    5. create a new ProgramParam ----------------------> QML create a new object
    6. copy construct len is 0 ----------------------> pass a empyt object to copy construct fucntion????? why?:confused:
    7. ConnectionBridge::appendParam p.size : 0 --------------------> call ConnectionBridge::appendParam, pass a empy ProgramParam object
    8. ProgramParamList::append p.size : 0 ----------------------> append "empty" ProgramParam object to list
    9. copy construct len is 0
    10. qml: after appendParam addressList
    11. qml: addrList: [1,4] -----------------------> infact, the address list should be [1,4], but QML pass [] to c++
    To copy to clipboard, switch view to plain text mode 


    Can any one tell me why?
    Attached Files Attached Files

  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 lost parameter when call a c++ function

    Because your function takes a non-const reference to a QObject and stores it's copy in a list. Change it to a pointer and store the pointer. Making copies of QObjects is not allowed.

    A side note - your code completely neglects the declarative nature of QML. It is not C++, you have to let go of your imperative habits.
    Last edited by wysota; 11th November 2014 at 09:07.
    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:

    Abel (12th November 2014)

  4. #3
    Join Date
    Aug 2013
    Posts
    27
    Thanks
    3

    Default Re: QMl lost parameter when call a c++ function

    wysota,
    Thank you very much! After changing function parameter to pointer, this problem is solved.
    I'm new comer to QML, so can you give me more details of
    A side note - your code completely neglects the declarative nature of QML. It is not C++, you have to let go of your imperative habits.
    ?

  5. #4
    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 lost parameter when call a c++ function

    I explained it many times on this forum so you might want to search for more complete explanation or just search the Web, but the main difference between imperative and declarative programming is that the latter describes the final effect of a program and the former provides detailed instructions (algorithm) that have to be executed to reach the objective. Instead of 20 or more lines of code of yours, you should specify that some object should have a specified set of properties under specific circumstances.
    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.


Similar Threads

  1. Replies: 2
    Last Post: 2nd December 2013, 05:43
  2. Replies: 4
    Last Post: 2nd August 2012, 08:42
  3. Function call with QFile as parameter?
    By Pyry in forum Newbie
    Replies: 2
    Last Post: 26th May 2011, 01:10
  4. skip function parameter
    By mattia in forum General Programming
    Replies: 4
    Last Post: 22nd November 2007, 16:55
  5. const function parameter problems
    By stevey in forum General Programming
    Replies: 3
    Last Post: 18th December 2006, 23:22

Tags for this Thread

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.