Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Can't access model variable from delegate
Forum Updated to NodeBB v4.3 + New Features

Can't access model variable from delegate

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HorstJ
    wrote on last edited by
    #1

    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));@

    QmlDataObject:
    @class QmlDataObject : public QObject
    {
    Q_OBJECT

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

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

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

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

    };@

    qml:
    @ Component {
    id: listDelegate
    Item {
    id: wrapper
    height: 26
    Column {
    Text {
    id: mytext
    color: "#FFFFFF"
    font.pixelSize: 12
    font.weight: Font.Bold
    height: 16
    text: model.name
    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".

    Regards
    Markus

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • H Offline
        H Offline
        HorstJ
        wrote on last edited by
        #3

        I have already tried with this result:

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved