Solved How to use C++ model in QML?
-
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 withText
. I want the source ofImage
ism_source
and the text ofText
ism_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 -
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 -
@lelev
Oh, thanks a lot. My bad ^^