Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

passing QList<QObject*> to lambda function



  • Hi, im tryng to create QObjectList-based Model like here http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qobjectlist-based-model

    but i have this error output when i use QList::append() in my lambda function connected to c++ signal :

    passing 'const QList<QObject*>' as 'this' argument discards qualifiers [-fpermissive]
                 dataList.append(new KemVar());
                                             ^
    
    //main.cpp
       QList<QObject*> dataList;
    
        QObject::connect(
            &fl, &FileLoader::objChanged,
            [=](const QString &newValue ) {
    
                QStringList fields = newValue.split("\r\n");
    
                   for (int i=0;i<fields.length();i++) {
                    qDebug()<< fields.at(i).toLocal8Bit().constData(); 
                 }
              //  dataList.append(new KemVar( fields.at(i).toLocal8Bit().constData(), fields.at(1).toLocal8Bit().constData())); // error here
        });
    
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("inspector",&insp);
        engine.rootContext()->setContextProperty("fl",&fl);
        engine.rootContext()->setContextProperty("myModel", QVariant::fromValue(dataList));
    

    Can someone help me solve this please ?

    This is my KemVar class (just QProperties Accessors and Mutators):

    lass KemVar : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString varId READ varId WRITE setVarId NOTIFY varIdChanged)
        Q_PROPERTY(QString varName READ varName WRITE setVarName NOTIFY varNameChanged)
        Q_PROPERTY(QString varAlias READ varAlias WRITE setVarAlias NOTIFY varAliasChanged)
        Q_PROPERTY(QString slaveId READ slaveId WRITE setSlaveId NOTIFY slaveIdChanged)
        Q_PROPERTY(QString storageType READ storageType WRITE setStorageType NOTIFY storageTypeChanged)
        Q_PROPERTY(QString dataType READ dataType WRITE setDataType NOTIFY dataTypeChanged)
        Q_PROPERTY(QString bitSize READ bitSize WRITE setBitSize NOTIFY bitSizeChanged)
        Q_PROPERTY(QString bitOffset READ bitOffset WRITE setBitOffset NOTIFY bitOffsetChanged)
        Q_PROPERTY(QString slaveFixAdr READ slaveFixAdr WRITE setSlaveFixAdr NOTIFY slaveFixAdrChanged)
        Q_PROPERTY(QString varType READ varType WRITE setVarType NOTIFY varTypeChanged)
        Q_PROPERTY(QString direction READ direction WRITE setDirection NOTIFY directionChanged)
        Q_PROPERTY(QString varIndex READ varIndex WRITE setVarIndex NOTIFY varIndexChanged)
        Q_PROPERTY(QString motionType READ motionType WRITE setMotionType NOTIFY motionTypeChanged)
        Q_PROPERTY(QString motionDirection READ motionDirection WRITE setMotionDirection NOTIFY motionDirectionChanged)
        Q_PROPERTY(QString motionIndex READ motionIndex WRITE setMotionIndex NOTIFY motionIndexChanged)
    
    
    public:
        explicit KemVar(QObject *parent = nullptr);
        KemVar(QString vid,
               QString vname,
               QString valias,
               QString vSlaveId,
               QString vStrotageType,
               QString vDataType,
               QString vBitSize,
               QString vBitOffset,
               QString vSlaveFixAdress,
               QString vVarType,
               QString vDirection,
               QString vVarIndex,
               QString vMotionType,
               QString vMotionDirection,
               QString vMotionIndex
               );
        QString varId(){
            return m_varId;
        }
        void setVarId(QString vid){
            m_varId=vid;
            emit varIdChanged(m_varId);
        }
        QString varName(){
            return m_name;
        }
        void setVarName(QString vid){
            m_name=vid;
            emit varNameChanged(m_name);
        }
        QString varAlias(){
            return m_alias;
        }
        void setVarAlias(QString al){
            m_alias=al;
            emit varAliasChanged(m_alias);
        }
        QString slaveId(){
            return m_slaviId;
        }
        void setSlaveId(QString sid){
            m_slaviId = sid;
            emit slaveIdChanged(sid);
        }
        QString storageType(){
            return m_storageType;
        }
        void setStorageType(QString st){
            m_storageType=st;
            emit storageTypeChanged(m_storageType);
        }
        QString dataType(){
            return m_dataType;
        }
        void setDataType(QString dt){
            m_dataType=dt;
            emit dataTypeChanged(m_dataType);
        }
        QString bitSize(){
            return m_bitSize;
        }
        void setBitSize(QString btsz){
            m_bitSize=btsz;
            emit bitSizeChanged(m_bitSize);
        }
        QString bitOffset(){
            return m_bitOffset;
        }
        void setBitOffset(QString bo){
            m_bitOffset=bo;
            emit bitOffsetChanged(m_bitOffset);
        }
        QString slaveFixAdr(){
            return m_slaveFixAdr;
        }
        void setSlaveFixAdr(QString fadr){
            m_slaveFixAdr=fadr;
            emit slaveFixAdrChanged(m_slaveFixAdr);
        }
        QString varType(){
            return m_type;
        }
        void setVarType(QString vt){
            m_type=vt;
            emit varTypeChanged(m_type);
        }
        QString direction(){
            return m_direction;
        }
        void setDirection(QString d){
            m_direction=d;
            emit directionChanged(m_direction);
        }
        QString varIndex(){
            return m_index;
        }
        void setVarIndex(QString vind){
            m_index=vind;
            emit varIndexChanged(m_index);
        }
        QString motionType(){
            return m_motionType;
        }
        void setMotionType(QString mot){
            m_motionType=mot;
            emit motionTypeChanged(m_motionType);
        }
        QString motionDirection(){
            return m_motionDirection;
        }
        void setMotionDirection(QString md){
            m_motionDirection=md;
            emit motionDirectionChanged(m_motionDirection);
        }
        QString motionIndex(){
            return m_motionIndex;
        }
        void setMotionIndex(QString mind){
            m_motionIndex=mind;
            emit motionIndexChanged(m_motionIndex);
        }
    
    
    
    
    signals:
        void varIdChanged(QString);
        void varNameChanged(QString);
        void varAliasChanged(QString);
        void slaveIdChanged(QString);
        void storageTypeChanged(QString);
        void dataTypeChanged(QString);
        void bitSizeChanged(QString);
        void bitOffsetChanged(QString);
        void slaveFixAdrChanged(QString);
        void varTypeChanged(QString);
        void directionChanged(QString);
        void varIndexChanged(QString);
        void motionTypeChanged(QString);
        void motionDirectionChanged(QString);
        void motionIndexChanged(QString);
    
    
    public slots:
    
    private:
        QString m_varId;
        QString m_name;
        QString m_alias;
        QString m_slaviId;
        QString m_storageType;
        QString m_dataType;
        QString m_bitSize;
        QString m_bitOffset;
        QString m_slaveFixAdr;
        QString m_type;
        QString m_direction;
        QString m_index;
        QString m_motionType;
        QString m_motionDirection;
        QString m_motionIndex;
    
    
    };
    #endif // KEMVAR_H
    
    


  • The error can be solved by replacing [=] with [&] and by removing the .toLocal8Bit().constData()s

    However, this does not work the way you are expecting it to work.

    QVariant::fromValue(dataList) crates a copy of dataList. Anything you do to dataList will not flow through into the QML side.

    You probably want to use a QAbstractItemModel instead, take a look at this wiki page


  • Moderators

    @LeLev I'm surpised it compiled.

    Is that supposed to be an overloaded constructor of KemVar?

    you should mark it with explicit, KemVar is of QObject and therefore must not be copied.
    secondly your overload has no parent parameter, do you even initialize Qbject, when that constructor is called ?



  • @J-Hilk @VRonin thx,
    @VRonin said in passing QList<QObject*> to lambda function:

    Anything you do to dataList will not flow through into the QML side.

    i will create this model once at start up and pass it to qml, nothing will be modified. So maybe i can try replacing [=] with [&] and by removing the .toLocal8Bit().constData()

    my files is momething like this :

    // file.txt

                 vid : firstid
                vname : name1
               valias : a1
               vSlaveId : s1
               vStrotageType : t1
           
                  vid : second
                vname : name2
               valias : a2
               vSlaveId : s2
               vStrotageType : t2
    

    i just need to show this in qml and be able to do dataBase like oerations , for exemple :
    show the vid of the element with alias "a1"

    but model will never change. Maybe there is Other approche for doing this ?

    @J.Hilk said in passing QList<QObject*> to lambda function:

    I'm surpised it compiled.

    it has not compiled

    @J.Hilk said in passing QList<QObject*> to lambda function:

    Is that supposed to be an overloaded constructor of KemVar?

    This is supposed to be my initializer constructor ..

    KemVar(QString vid,
    QString vname,
    QString valias,
    QString vSlaveId,
    QString vStrotageType,
    QString vDataType,
    QString vBitSize,
    QString vBitOffset,
    QString vSlaveFixAdress,
    QString vVarType,
    QString vDirection,
    QString vVarIndex,
    QString vMotionType,
    QString vMotionDirection,
    QString vMotionIndex
    );



  • @LeLev said in passing QList<QObject*> to lambda function:

    i will create this model once at start up and pass it to qml, nothing will be modified

    Then it will be empty. fl will not emit objChanged before engine.rootContext()->setContextProperty("myModel", QVariant::fromValue(dataList)); unless you are running a very unhealthy race condition in a secondary thread



  • I finally used QAbstractItemModel thx


Log in to reply