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.
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qRegisterMetaType<ProgramParam>("ProgramParam");
qRegisterMetaType<ProgramParamList>("ProgramParamList");
qmlRegisterType<ProgramParam>("ProgramParam",1,0,"ProgramParam");
qmlRegisterType<ConnectionBridge>("ConnectionBridge",1,0,"ConnectionBridge");
QQmlApplicationEngine engine;
engine.
load(QUrl(QStringLiteral
("qrc:///main.qml")));
return app.exec();
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qRegisterMetaType<ProgramParam>("ProgramParam");
qRegisterMetaType<ProgramParamList>("ProgramParamList");
qmlRegisterType<ProgramParam>("ProgramParam",1,0,"ProgramParam");
qmlRegisterType<ConnectionBridge>("ConnectionBridge",1,0,"ConnectionBridge");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
In ConnectionBridg class, I export a function to append param to paramList
Q_INVOKABLE void appendParam(ProgramParam p);
void ConnectionBridge::appendParam(ProgramParam p)
{
qDebug() << "ConnectionBridge::appendParam p.size :" << p.m_addrList.size();
m_programParamList.append(p);
}
void ProgramParamList::append(ProgramParam &p)
{
qDebug() << "ProgramParamList::append p.size :" << p.m_addrList.size();
m_programParamList.append(p);
}
Q_INVOKABLE void appendParam(ProgramParam p);
void ConnectionBridge::appendParam(ProgramParam p)
{
qDebug() << "ConnectionBridge::appendParam p.size :" << p.m_addrList.size();
m_programParamList.append(p);
}
void ProgramParamList::append(ProgramParam &p)
{
qDebug() << "ProgramParamList::append p.size :" << p.m_addrList.size();
m_programParamList.append(p);
}
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.
var programConfig = prepareOneTypeProgramConfig(i);
if(!programConfig.isEmpty())
{
// console.log("add to myltyProgramParam!");
// programConfig.print();
var aProgramParam = Qt.createQmlObject('import ProgramParam 1.0; ProgramParam {}', buttonProgram, "dynamicSnippet1");
aProgramParam.addrList = programConfig.addrList;
console.log("update addressList");
aProgramParam.controlerType = programConfig.controlerType;
aProgramParam.flashFilePath = programConfig.flashFilePath;
aProgramParam.eepFilePath = programConfig.eepFilePath;
aProgramParam.bUpdateEEP = programConfig.bUpdateEEP;
aProgramParam.bUpdateFlash = programConfig.bUpdateFlash;
aProgramParam.bUpdateFactorySetting = programConfig.bUpdateFactory;
aProgramParam.factorySettingPath = programConfig.factoryFilePath;
aProgramParam.bUpdateSN = programConfig.bUpdateSN;
aProgramParam.firstSN = programConfig.sn;
aProgramParam.bNeedSyncTime = programConfig.bNeedTimeSync;
aProgramParam.bNeedResorteSetting = programConfig.bNeedRestoreCustomerSetting;
console.log("before appendParam addressList");
aConnectionBridge.appendParam(aProgramParam);
var programConfig = prepareOneTypeProgramConfig(i);
if(!programConfig.isEmpty())
{
// console.log("add to myltyProgramParam!");
// programConfig.print();
var aProgramParam = Qt.createQmlObject('import ProgramParam 1.0; ProgramParam {}', buttonProgram, "dynamicSnippet1");
aProgramParam.addrList = programConfig.addrList;
console.log("update addressList");
aProgramParam.controlerType = programConfig.controlerType;
aProgramParam.flashFilePath = programConfig.flashFilePath;
aProgramParam.eepFilePath = programConfig.eepFilePath;
aProgramParam.bUpdateEEP = programConfig.bUpdateEEP;
aProgramParam.bUpdateFlash = programConfig.bUpdateFlash;
aProgramParam.bUpdateFactorySetting = programConfig.bUpdateFactory;
aProgramParam.factorySettingPath = programConfig.factoryFilePath;
aProgramParam.bUpdateSN = programConfig.bUpdateSN;
aProgramParam.firstSN = programConfig.sn;
aProgramParam.bNeedSyncTime = programConfig.bNeedTimeSync;
aProgramParam.bNeedResorteSetting = programConfig.bNeedRestoreCustomerSetting;
console.log("before appendParam addressList");
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:
create a new ProgramParam ---------------------> creat a new object in QML
set address! size is 2 ---------------------> assigne data, here address list size is 2
qml: update addressList
qml: before appendParam addressList ----------------------> before call aConnectionBridge.appendParam(aProgramParam);
create a new ProgramParam ----------------------> QML create a new object
copy construct len is 0 ----------------------> pass a empyt object to copy construct fucntion????? why?:confused:
ConnectionBridge::appendParam p.size : 0 --------------------> call ConnectionBridge::appendParam, pass a empy ProgramParam object
ProgramParamList::append p.size : 0 ----------------------> append "empty" ProgramParam object to list
copy construct len is 0
qml: after appendParam addressList
qml: addrList: [1,4] -----------------------> infact, the address list should be [1,4], but QML pass [] to c++
create a new ProgramParam ---------------------> creat a new object in QML
set address! size is 2 ---------------------> assigne data, here address list size is 2
qml: update addressList
qml: before appendParam addressList ----------------------> before call aConnectionBridge.appendParam(aProgramParam);
create a new ProgramParam ----------------------> QML create a new object
copy construct len is 0 ----------------------> pass a empyt object to copy construct fucntion????? why?:confused:
ConnectionBridge::appendParam p.size : 0 --------------------> call ConnectionBridge::appendParam, pass a empy ProgramParam object
ProgramParamList::append p.size : 0 ----------------------> append "empty" ProgramParam object to list
copy construct len is 0
qml: after appendParam addressList
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?
Bookmarks