I have a database full of albums and i want to display these albums in a GridView
. I created a qml file that takes a personalized module (albumObject.h
) in order to get the data from the database. Since I already have a MainWindow
in main.cpp
i need to display the GridView
in a QQuickWidget
that is constructed inside the mainwindow.cpp
file. Here's is the code:
albumObject.h
class AlbumObject : public QObject {
Q_OBJECT
Q_PROPERTY(QString artist READ getArtist)
Q_PROPERTY(QString album READ getAlbum)
Q_PROPERTY(QString date READ getDate)
Q_PROPERTY(QString img READ getImg)
public:
AlbumObject(QObject* parent = nullptr);
const QString getArtist() const;
const QString getAlbum() const;
const QString getDate() const;
const QString getImg() const;
void setArtist(const QString& artist);
void setAlbum(const QString& album);
void setDate(const QString& date);
void setImg(const QString& img);
private:
QString m_artist;
QString m_album;
QString m_date;
QString m_img;
};
mainwindow.cpp
I use this connect inside the MainWindow constructor to update the UI when all the albums are added to the database
connect(&databaseManager, &DatabaseManager::albumAddedToDB,
&databaseManager, [&]() {
QList<QObject*> albums = databaseManager.getAlbumsFromDB();
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
QQmlContext *ctxt = view.rootContext();
ctxt->setContextProperty("albumModel", QVariant::fromValue(albums));
ui->albumGrid->setSource(QUrl{"../Hallownest/Model/something.qml"});
ui->albumGrid->createWindowContainer(&view, this);
});
something.qml
import QtQuick 2.4
Rectangle {
width: 800; height: 600
Component {
id: albumDelegate
Item {
width: grid.cellWidth; height: grid.cellHeight
Column {
anchors.fill: parent
Text { text: artist; anchors.horizontalCenter: parent.horizontalCenter }
Text { text: album; anchors.horizontalCenter: parent.horizontalCenter }
Text { text: date; anchors.horizontalCenter: parent.horizontalCenter }
}
}
}
GridView {
id: grid
anchors.fill: parent
cellWidth: 150; cellHeight: 150
model: albumModel
delegate: albumDelegate
highlight: Rectangle {color: "lightsteelblue"; radius: 5 }
focus: true;
}
}
When i launch the program i get this error:
ReferenceError: albumModel is not defined
In similar questions most of the users have this problem because they set the context after loading the qml file but this is not my case.
QQuickView view;
toQQuickView *view = new QQuickView;
and provide a minimal reproducible example – eyllanesc