Qml and C++ transfer arrays, structures, and data to each other

Qml and C++ transfer arrays, structures, and data to each other

There are many methods, but I only use QVariantMap here, which can pass single, multiple, structure, etc.

1. First, C++ is passed to QML, for example, the structure is converted into map:

QVariantMap DataSource::readSystemConfig()
{
    QVariantMap  mapConfig;
    int ret = 0;
 
    // 要转换一下
    if(ret == 0){
        //DBG("struct 变成 map");
        mapConfig.insert("adcLevel",m_sysConfig.adc_level);
        mapConfig.insert("adcDigit",m_sysConfig.adc_digit);
        mapConfig.insert("adcChs",m_sysConfig.adc_chs);
        mapConfig.insert("outLevelSet",m_sysConfig.out_level_set);
        mapConfig.insert("outIoVal",m_sysConfig.out_io_val);
        mapConfig.insert("canBd",m_sysConfig.can_bd);0
    }0
    return mapConfig;
}

After registering the C++ object in QML, directly call the C++ function to obtain this map:

    onClicked: {
            var i=0;
            var cfg = cppObject.readSystemConfig();
 
            // 更新到控件中 mcuConfigPage
            for(var key in cfg){
                i++;
                mcuConfigPage.setText(key,cfg[key])
            }
            if(i == 0)
                toast.show("获取失败")
        }

2. Then QML obtains the data and sends it back to C++:

    function getAllText(){
 
        // 主要要有个空括号表示这个是map
        var map={};
 
        map["adcLevel"] = adcLevel.getText();
        map["adcDigit"] = adcDigit.getText();
        map["adcChs"] = adcChs.getText();
        map["outLevelSet"] = outLevelSet.getText();
        map["outIoVal"] = outIoVal.getText();
        map["canBd"] = canBd.getText();
        return map;
    }
     
    // qml中这样调用:
    onSelectionChanged:{
            var map;
 
            map = mcuConfigPage.getAllText();
            if(dataSource.setSystemConfig(map) === false)
                toast.show("操作失败")
        }

C++ method:

bool DataSource::setSystemConfig(const QVariantMap mapConfig)
{
    m_sysConfig.adc_level = mapConfig.value("adcLevel").toUInt();
    m_sysConfig.adc_digit = mapConfig.value("adcDigit").toUInt();
    m_sysConfig.adc_chs = mapConfig.value("adcChs").toUInt();
    m_sysConfig.out_level_set = mapConfig.value("outLevelSet").toUInt();
    m_sysConfig.out_io_val = mapConfig.value("outIoVal").toUInt();
    m_sysConfig.can_bd = mapConfig.value("canBd").toUInt();
 
 
    return m_abif->setSystemConfig(m_sysConfig) == ABI_NONE;
}

=================Other methods to explore================

1. Define the structure with Q_GADGET and Q_PROPERTY, and then you can directly access the structure members (aliases). See for details:

qt - Best way to access a cpp structure in QML - Stack Overflow

2. Use QJSValue for reference transfer in C++

qt - How to pass data from QML to C++ as a mutable reference - Stack Overflow

Declaration:
Q_INVOKABLE void select_company(int index,QJSValue out);
Definition:
void Companies::select_company(int index,QJSValue out) {
 
    out.setProperty("company_name","Acme, Inc.");
    out.setProperty("identity_id",29673);
}
QML:
var retval={};
data_model.select_company(index,retval);
console.log(retval.company_name);
console.log(retval.identity_id);

3. Use callback functions in QML to return C++ data:

qt - Passing a Javascript callback to a C++ Invoked method in Qml - Stack Overflow

4. Official: Data Type Conversion Between QML and C++ | Qt QML 5.15.14

As a benefit for this article, you can receive a Qt development learning package and technical videos for free , including (Qt practical projects, C++ language basics, C++ design patterns, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project practice, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click at the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/133382624