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 use C++ model in QML?
Forum Updated to NodeBB v4.3 + New Features

How to use C++ model in QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 345 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.
  • Lucas_1603L Offline
    Lucas_1603L Offline
    Lucas_1603
    wrote on last edited by
    #1

    I have a class Song in C++:
    Here is the header file: song.h

    #include <QAbstractListModel>
    #include <QScopedPointer>
    class Song
    {
    public:
        Song(const QString &title, const QString &singer, const QString &source, const QString &albumArt);
    
        QString title() const;
        QString singer() const;
        QString source() const;
        QString album_art() const;
    
    private:
        QString m_title;
        QString m_singer;
        QString m_source;
        QString m_albumArt;
    };
    

    And here is the source file: song.cpp

    #include <QFileInfo>
    #include <QUrl>
    #include <QMediaPlaylist>
    #include "song.h"
    Song::Song(const QString &title, const QString &singer, const QString &source, const QString &albumArt)
    {
        m_title = title;
        m_singer = singer;
        m_source = source;
        m_albumArt = albumArt;
    }
    
    QString Song::title() const
    {
        return m_title;
    }
    
    QString Song::singer() const
    {
        return m_singer;
    }
    
    QString Song::source() const
    {
        return m_source;
    }
    
    QString Song::album_art() const
    {
        return m_albumArt;
    }
    

    How can I use this class as a model in a ListView of QML? This ListView has a component with an item: Image and a delegate with Text. I want the source of Image is m_source and the text of Text is m_title. I don't know how to access the methods and attributes of C++ from QML.
    Hope you guys help me deal with it. Thanks a lot

    ODБOïO 1 Reply Last reply
    0
    • Lucas_1603L Lucas_1603

      I have a class Song in C++:
      Here is the header file: song.h

      #include <QAbstractListModel>
      #include <QScopedPointer>
      class Song
      {
      public:
          Song(const QString &title, const QString &singer, const QString &source, const QString &albumArt);
      
          QString title() const;
          QString singer() const;
          QString source() const;
          QString album_art() const;
      
      private:
          QString m_title;
          QString m_singer;
          QString m_source;
          QString m_albumArt;
      };
      

      And here is the source file: song.cpp

      #include <QFileInfo>
      #include <QUrl>
      #include <QMediaPlaylist>
      #include "song.h"
      Song::Song(const QString &title, const QString &singer, const QString &source, const QString &albumArt)
      {
          m_title = title;
          m_singer = singer;
          m_source = source;
          m_albumArt = albumArt;
      }
      
      QString Song::title() const
      {
          return m_title;
      }
      
      QString Song::singer() const
      {
          return m_singer;
      }
      
      QString Song::source() const
      {
          return m_source;
      }
      
      QString Song::album_art() const
      {
          return m_albumArt;
      }
      

      How can I use this class as a model in a ListView of QML? This ListView has a component with an item: Image and a delegate with Text. I want the source of Image is m_source and the text of Text is m_title. I don't know how to access the methods and attributes of C++ from QML.
      Hope you guys help me deal with it. Thanks a lot

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      hi
      @lucas_1603 said in How to use C++ model in QML?:

      how to access the methods and attributes of C++ from QML.

      google it quickly
      https://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html

      Lucas_1603L 1 Reply Last reply
      1
      • ODБOïO ODБOï

        hi
        @lucas_1603 said in How to use C++ model in QML?:

        how to access the methods and attributes of C++ from QML.

        google it quickly
        https://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html

        Lucas_1603L Offline
        Lucas_1603L Offline
        Lucas_1603
        wrote on last edited by
        #3

        @lelev
        Oh, thanks a lot. My bad ^^

        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