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

[SOLVED] Accessing properties in QML Delegate for imported C++ objects / ListView



  • Hello,

    I am making a parameter edit screen with QML, QT 5.3. I am importing a C++ map of objects as a Listview model, and it almost works : All rectangles appear, but no text in it.
    I get these QML errors. "ReferenceError: prop_name is not defined" "ReferenceError: prop_description is not defined"
    It seems the list exist, was constructed correctly with the right number of items, but I cannot access the properties. Probably I am still doing something wrong, but I can't find what, from all information I find online, I think my effort could work, but it does not. I implemented a workaround to get the data from a C++ map into a variant list, and this seems ok, but probably I still made a mistake. I don't see it.

    I'd appreciate if you experts had a look, and tell me your opinion, how can I make this work ?
    Is this the wrong approach ?

    You can see the code fragments below, ParamScreen.qml is included in
    marpgui.qml :

    @// main initialisation :

    QMap<QString, MaQParam *> marpParamMap;
    QVariantList marpParamList;

    MARPGUI::MARPGUI(JSONRPCUDP* param_jsonrpcserver)
    {
    MARP_jsonrpcserver = param_jsonrpcserver;
    GUIRootContext = viewer.rootContext();
    qmlRegisterType<MaQParam>("MaQParam", 1, 0, "MaQParam");
    GUIRootContext->setContextProperty("marpParamList", QVariant::fromValue(marpParamList));
    viewer.setMainQmlFile(QStringLiteral("qml/QT_UDP_QML/MARPGUI.qml"));
    viewer.showExpanded();
    GUIRootObject = viewer.rootObject();
    }

    //--- maqparam.h

    #ifndef MAQPARAM_H
    #define MAQPARAM_H

    #include <QObject>

    class MaQParam : public QObject
    {
    Q_OBJECT
    public:
    explicit MaQParam(QObject *parent=0, QString par_name="unknown",QString par_value="0",QString par_min="0",QString par_max="100", QString par_description="no comment");
    Q_PROPERTY(QString prop_name READ getname NOTIFY nameChanged)
    Q_PROPERTY(QString prop_value MEMBER m_value )
    Q_PROPERTY(QString prop_min MEMBER m_min )
    Q_PROPERTY(QString prop_max MEMBER m_max )
    Q_PROPERTY(QString prop_description MEMBER m_description)
    QString getname(void);
    signals:
    void nameChanged();
    public slots:

    private:
    QString m_name;
    QString m_value;
    QString m_min;
    QString m_max;
    QString m_description;
    };

    #endif // MAQPARAM_H

    //--- maqparam.c

    #include "maqparam.h"

    /*
    MaQParam::MaQParam(QObject *parent) : QObject(parent)
    {
    }
    */

    MaQParam :: MaQParam(QObject *parent, QString par_name,QString par_value,QString par_min,QString par_max, QString par_description) : QObject(parent)
    {
    m_name = par_name;
    m_value = par_value;
    m_min = par_min;
    m_max = par_max;
    m_description = par_description;
    }

    QString MaQParam::getname(void)
    {
    return(m_name);
    }

    // ParamScreen.qml

    import QtQuick 2.2
    import QtQuick.Dialogs 1.1
    import QtGraphicalEffects 1.0
    import QtQuick.Controls 1.2
    import QtQuick.Controls.Styles 1.2
    import QtQuick.Layouts 1.1
    import MaQParam 1.0

    Rectangle {
    x: 0
    y: 0
    anchors.fill: parent
    gradient: Gradient {
    GradientStop {
    id: gradientStop3
    position: 0
    color: "#5d62cb"
    }

        GradientStop {
            position: 1
            color: "#d7d9fa"
        }
    }
    
    Button {
        id: closeparamlist_button
        x: 300
        y: 325
        width: 50
        text: qsTr("Close")
        onClicked : parent.z = -1;
    }
    
    
    RowLayout {
        id: parameditrowlayout
        anchors.fill: parent
    
        Component {
            id: paramlijstDelegate
            Row {
                id: paramlijst
                Rectangle {
                    width: 250
                    height: 60
                    color: "dark blue"
                    radius : 5
                    MouseArea{
                           id: paramlistMouseArea
                           anchors.fill: parent
                           }
    
                    Column {
                        width: parent.width
                        anchors.horizontalCenter: parent.horizontalCenter
                        Text { width: parent.width; anchors.horizontalCenter: parent.horizontalCenter; font.pointSize : 16 ; horizontalAlignment:Text.AlignHCenter; text: prop_name; color: paramlijst.ListView.view.paramlijst_color   }
                        Text { width: parent.width;  anchors.horizontalCenter: parent.horizontalCenter; font.pointSize : 16 ; horizontalAlignment:Text.AlignHCenter; text: prop_description; color: paramlijst.ListView.view.paramlijst_color   }
                    }
                }
    
            }
        }
    
    
    
        ListView {
            width : 260
            height : 360
            anchors.top : parameditrowlayout.top
            anchors.left: parameditrowlayout.left
            property color paramlijst_color: "white"
            //model: paramlijstModel
            model: marpParamList
            delegate: paramlijstDelegate
            anchors.margins: 20
            clip: true
            spacing: 5
    
        }
        Item{
            id: editWindow
            anchors.top : parameditrowlayout.top
            anchors.left: parameditrowlayout.left + parameditrowlayout.width/2
            width : 350
            ParamEdit{
                id: paramedit
    
            }
    
        }
    
    }
    

    }

    // adding new parameters happens with this code (if a parameter exists it is updated instead of added)

       MaQParam * pQParam;
        pQParam = new MaQParam((QObject *)0,nameStr,valueStr,minStr,maxStr,descriptionStr);
        marpParamMap.insert(nameStr,pQParam);
        marpParamList.clear();
        foreach (MaQParam * curQParam,marpParamMap.values())
    
        {
            marpParamList.append( QVariant::fromValue(curQParam));
        }
        GUIRootContext->setContextProperty("marpParamList", QVariant::fromValue(marpParamList));
    

    ...
    @





  • Hi,
    I found the solution, it was something really simple i had overlooked :
    It should access the properties through model.modelData
    so use model.modelData.prop_name instead of prop_name

    It works now, the delegate becomes :
    @Component {
    id: paramlijstDelegate
    Row {
    id: paramlijst
    Rectangle {
    width: 250
    height: 60
    color: "#353535"
    radius : 5
    MouseArea{
    id: paramlistMouseArea
    anchors.fill: parent
    }

                    Column {
                        width: parent.width
                        anchors.horizontalCenter: parent.horizontalCenter
                        Text { width: parent.width; anchors.horizontalCenter: parent.horizontalCenter; font.pointSize : 18 ; horizontalAlignment:Text.AlignHCenter; text : model.modelData.prop_name; color: "#00B4FF"   }
                        Text { width: parent.width;  anchors.horizontalCenter: parent.horizontalCenter; font.pointSize : 14 ; horizontalAlignment:Text.AlignHCenter; text:  model.modelData.prop_value; color: "#FFFFFF" }
                    }
                }
    
            }
        }
    

    @



  • Thanks Vincent, for the suggestion to use qabstractitemmodel.
    It looks suitable. When I find the time, I'll try this too, it may be more efficient.


Log in to reply