gpt4 book ai didi

c++ - 将 C++ 模块连接到 QML - ReferenceError : is not defined

转载 作者:行者123 更新时间:2023-11-30 04:53:10 25 4
gpt4 key购买 nike

我有一个装满相册的数据库,我想在 GridView 中显示这些相册。我创建了一个 qml 文件,该文件采用个性化模块 (albumObject.h),以便从数据库中获取数据。因为我在 main.cpp 中已经有一个 MainWindow 我需要在构造的 QQuickWidget 中显示 GridViewmainwindow.cpp 文件中。这是代码:

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;
};

主窗口.cpp

当所有专辑都添加到数据库时,我在 MainWindow 构造函数中使用此连接来更新 UI

  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;
}
}

当我启动程序时出现此错误:

ReferenceError: albumModel is not defined

在类似的问题中,大多数用户都有这个问题,因为他们在加载 qml 文件后设置了上下文,但我的情况不是这样。

最佳答案

你的问题是你创建了一个 QQuickView 不必要地认为这样你会得到 QQmlContext 但它是不正确的,QQuikWidget 已经有它自己的 QQmlContext。另一方面,列表最好是一个属性并导出 MainWindow,这样您就可以访问该属性。

*.h

class MainWindow : public QMainWindow
{
Q_OBJECT
Q_PROPERTY(QList<QObject *> albums READ getAlbums NOTIFY albumsChanged)

public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
QList<QObject *> getAlbums() const;
void updateAlbums();
signals:
void albumsChanged();
private:
Ui::MainWindow *ui;
QList<QObject *> m_albums;
DatabaseManager databaseManager;
};

*.cpp

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->albumGrid->rootContext()->setContextProperty("mainwindow", this);
ui->albumGrid->setSource(QUrl{"../Hallownest/Model/something.qml"});
connect(&databaseManager, &DatabaseManager::albumAddedToDB, this, &MainWindow::updateAlbums);
}

MainWindow::~MainWindow()
{
delete ui;
}

QList<QObject *> MainWindow::getAlbums() const
{
return m_albums;
}

void MainWindow::updateAlbums()
{
qDeleteAll(m_albums);
m_albums.clear();
m_albums = databaseManager.getAlbumsFromDB();
emit albumsChanged();
}

*.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: modelData.artist; anchors.horizontalCenter: parent.horizontalCenter }
Text { text: modelData.album; anchors.horizontalCenter: parent.horizontalCenter }
Text { text: modelData.date; anchors.horizontalCenter: parent.horizontalCenter }
}
}
}

GridView {
id: grid
anchors.fill: parent
cellWidth: 150; cellHeight: 150
model: mainwindow.albums
delegate: albumDelegate
highlight: Rectangle {color: "lightsteelblue"; radius: 5 }
focus: true;
}
}

关于c++ - 将 C++ 模块连接到 QML - ReferenceError : <blank> is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53936100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com