Important: Please read the Qt Code of Conduct -

Can't access model variable from delegate

  • Hi everyone,
    playing with qml a bit I came to another problem i can't solve by myself. My data should be presented via a listview component, but I'm not able to access the variables from within the delegate. I declared the datamodel like this:
    @Q_PROPERTY(QVariant dataModel READ getDataModel NOTIFY dataModelChange)@

    and the getter looks like this:
    @ QList<QObject*> data;

    data.append(new QmlDataObject("first", "test1"));
    data.append(new QmlDataObject("second", "test2"));
    data.append(new QmlDataObject("third", "test3"));

    return (QVariant::fromValue(data));@

    @class QmlDataObject : public QObject

    Q_PROPERTY(QString name READ getName);
    Q_PROPERTY(QVariant value READ getValue);

    QmlDataObject(QString name, QVariant value);
    virtual ~QmlDataObject();
    void setValue(QVariant value);

    void nameChanged(QString);
    void valueChanged(QVariant);

    QString name;
    QVariant value;
    QString getName() const { return (name);}
    QVariant getValue() const { return (value);}


    @ Component {
    id: listDelegate
    Item {
    id: wrapper
    height: 26
    Column {
    Text {
    id: mytext
    color: "#FFFFFF"
    font.pixelSize: 12
    font.weight: Font.Bold
    height: 16
    verticalAlignment: Text.AlignVCenter
    horizontalAlignment: Text.AlignHCenter
    Text {
    id: mytext2
    color: "#FFFFFF"
    font.pixelSize: 10
    font.weight: Font.Normal
    height: 10
    text: model.value
    verticalAlignment: Text.AlignVCenter
    horizontalAlignment: Text.AlignHCenter

    The datamodel seems to be accessable, as I'm getting 3 times
    "Unable to assign [undefined] to QString text
    Unable to assign [undefined] to QString text".


  • Since i remember you have to use for example text: name instead of text: .

  • I have already tried with this result:

    ReferenceError: Can't find variable: value
    ReferenceError: Can't find variable: name

Log in to reply