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. How to access (c++) model data from qml
QtWS25 Last Chance

How to access (c++) model data from qml

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 2.5k Views
  • 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.
  • F Offline
    F Offline
    Furby
    wrote on last edited by
    #1

    Hi,

    I'm new with qlm and I have a problem to access data from a Subclass in my ListModel (derived from QAbstractListModel ).
    I use the ListModel with a ListView and everything is fine. But I also wanna access the Data outside of the ListView. For this reason I use a public slot to get the data from my (c++) ListModel dependent on the current selected item in the ListView. This part also works fine, but from this point I could not access the explicit model (entry) stored in my ListModel

    Here some code snippets:
    I hope your guys can help me.

    main.cpp
    @
    ListModel *listModel = new ListModel();
    listModel->add(new Subclass("Class 1", "Class 1.1", "Class 1.2") );

    // qmlRegisterInterface<ListModel>("ListModel");
    qmlRegisterInterface<Parent>("Parent");
    qmlRegisterInterface<Subclass>("Subclass");

     qRegisterMetaType<Parent>("Parent");
     qRegisterMetaType<Parent>("Subclass");
    
    QtQuick2ApplicationViewer viewer;
    viewer.rootContext()->setContextProperty("listModel", listModel);
    
    viewer.setMainQmlFile&#40;QStringLiteral("qml/test/main.qml"&#41;);
    viewer.showExpanded();
    

    @

    parent.h
    @
    class Parent : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString firstname READ getFirstname WRITE setFirstname NOTIFY firstNameChanged )
    Q_PROPERTY(QString lastname READ getLastname WRITE setLastname NOTIFY lastNameChanged)

    public:
    explicit Parent(){}
    explicit Parent(const QString &first, const QString &last,QObject *parent = 0);
    Parent( const Parent &p);
    Parent& operator=(const Parent & p);

    signals:

    public slots:
    QString getFirstname() const;
    QString getLastname() const ;

    void setFirstname( const QString &firstname);
    void setLastname( const QString &lastname);
    

    private:
    QString _firstname;
    QString _lastname;

    signals:
    void firstNameChanged();
    void lastNameChanged();

    };

    Q_DECLARE_METATYPE( Parent )
    @

    @
    class Subclass : public Parent
    {
    Q_OBJECT

    Q_PROPERTY(QString scublassname READ getSubclassName WRITE setSubclassName NOTIFY subclassNameChanged)
    

    public:
    Subclass(){}
    Subclass( const QString &last, const QString &first, const QString &sub);
    Subclass( const Subclass &subclass);
    Subclass& operator=(const Subclass & subclass);

    public slots:
    QString getSubclassName() const ;
    void setSubclassName(const QString & name);

    private:
    QString _className;

    signals:
    void subclassNameChanged();
    };

    Q_DECLARE_METATYPE( Subclass )
    @

    main.qml
    @
    import QtQuick 2.0

    Rectangle {
    width: 360
    height: 360

    Component
    {
        id: decorator
        Rectangle{
            height: 100
            width: 360
    
            color: "yellow"
            opacity: 0.5
    
            Text {
             text:  '<b>Firstname:</b> ' + model.firstname1 + '<br>' +
                    '<b>Lastname:</b> ' + model.lastname1 + '<br>' +
                    '<b>SubclassName:</b> ' + model.scublassname1 + '<br>'
            }
    
            MouseArea {
                anchors.fill: parent;
                onClicked:{
                    listview.currentIndex = index;
                }
            }
        }
    }
    
    ListView{
        id: listview
       anchors.top: parent.top
       anchors.left: parent.left
       anchors.right: parent.right
    
        height: 200
        model: listModel
        delegate: decorator
        spacing: 15
        focus: true
        clip: true
    
    }
    
    Rectangle{
        color: "blue"
        anchors.top: listview.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        height: 160
    
        Text{
            anchors.fill: parent
            text: {
                "index =["+listview.currentIndex+"]  " +  listModel.get(listview.currentIndex).firstname
            }
        }
    }
    

    }
    @

    1 Reply Last reply
    0
    • P Offline
      P Offline
      portoist
      wrote on last edited by
      #2

      How is defined "get" method in your ListModel? You have mentioned that you are using public slot, but slot should return void. If you need to expose non-void method to QML you need to define it like this:
      @
      ...
      public:
      Q_INVOKABLE Parent* get(const int &index);
      ...
      @

      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