将 Combobox 与 QStringList 模型一起使用时出现 QML 错误 "modelData is not defined"
QML Error "modelData is not defined" when using Combobox with QStringList model
我是 Qt 和 QML 的新手。我想使用带有 QStringList 模型的组合框来显示我的列表。但是,它根本不起作用。下面是相关的源码。
ListModelResourceManager.hpp
class ListModelResourceManager : public QObject {
Q_OBJECT
Q_PROPERTY(QStringList model MEMBER m_model NOTIFY modelChanged)
QStringList m_model;
public:
ListModelResourceManager(const QString& ctx_id, const QQmlApplicationEngine& engine, QObject* parent = nullptr);
public slots:
void update();
signals:
void modelChanged();
};
main.cpp
...
ListModelResourceManager lmResourceManager("MODEL_ResourceManagerList", engine);
...
engine.load(QUrl(QStringLiteral("qrc:/viewmain.qml")));
viewmain.qml
ComboBox {
id: idResourceList
height: 30
visible: true
//model: ["First", "Second", "Third"] // IT WORKS well!!!
model : MODEL_ResourceManagerList.model
onFocusChanged: {
MODEL_ResourceManagerList.update();
}
delegate: ItemDelegate {
width: parent.width
text: modelData
font.weight: idResourceList.currentIndex === index ? Font.DemiBold : Font.Normal
font.pixelSize: 30
highlighted: idResourceList.highlightedIndex == index
}
使用注释模型定义时(模型:["First"、"Second"、"Third"])效果很好。
请让我知道我的代码部分有什么问题。谢谢
我发现了我的错误,所以我自己回复了我的答案。
要注册模型以查看组件,注册对象必须是模型类型class。就我而言,我的 ListModelResourceManager 不是模型 class。所以需要使用 QStringListModel 作为模型 class 而不是 link 这个 QStringListModel 对象到 QML Combobox 组件。
我在这里为像我一样感到痛苦的人更新了我的工作源
ListModelResourceManager.hpp(管理QStringListMoel)
class ListModelResourceManager : public QObject {
Q_OBJECT
QStringListModel m_model;
public:
ListModelResourceManager(const QString& model_id, const QString& ctx_id, const QQmlApplicationEngine& engine, QObject* parent = nullptr);
};
ListModelResourceManager.cpp(m_model通过注册到本地资源管理器的回调函数更新)
ListModelResourceManager::ListModelResourceManager(const QString& model_id, const QString& ctx_id, const QQmlApplicationEngine& engine, QObject* parent)
: QObject(parent), m_model()
{
engine.rootContext()->setContextProperty(ctx_id, this);
engine.rootContext()->setContextProperty(model_id, &m_model);
rutResource::ResourceManager::getInstance().addResourceUpdatedListener([this](){
auto resources = rutResource::ResourceManager::getInstance().resources();
QStringList list;
for (auto item : resources)
{
list.append(QString::fromUtf8(item.first.c_str()));
}
m_model.setStringList(list);
});
}
main.cpp
int main(int argc, char *argv[])
{
// ...
ListModelResourceManager lmResourceManager("MODEL_Resources","ResourceManagerList", engine);
// ...
}
view.qml
// ...
ComboBox {
id : idResourceSelector
model : MODEL_Resources
textRole: "display"
}
// ...
ps。我想将我的本机资源管理器与 QT 界面分开,所以我使用回调侦听器。如果不是您的情况,您可以使用其他方法更新您的模型,例如 signal/slot 等...
我是 Qt 和 QML 的新手。我想使用带有 QStringList 模型的组合框来显示我的列表。但是,它根本不起作用。下面是相关的源码。
ListModelResourceManager.hpp
class ListModelResourceManager : public QObject {
Q_OBJECT
Q_PROPERTY(QStringList model MEMBER m_model NOTIFY modelChanged)
QStringList m_model;
public:
ListModelResourceManager(const QString& ctx_id, const QQmlApplicationEngine& engine, QObject* parent = nullptr);
public slots:
void update();
signals:
void modelChanged();
};
main.cpp
...
ListModelResourceManager lmResourceManager("MODEL_ResourceManagerList", engine);
...
engine.load(QUrl(QStringLiteral("qrc:/viewmain.qml")));
viewmain.qml
ComboBox {
id: idResourceList
height: 30
visible: true
//model: ["First", "Second", "Third"] // IT WORKS well!!!
model : MODEL_ResourceManagerList.model
onFocusChanged: {
MODEL_ResourceManagerList.update();
}
delegate: ItemDelegate {
width: parent.width
text: modelData
font.weight: idResourceList.currentIndex === index ? Font.DemiBold : Font.Normal
font.pixelSize: 30
highlighted: idResourceList.highlightedIndex == index
}
使用注释模型定义时(模型:["First"、"Second"、"Third"])效果很好。
请让我知道我的代码部分有什么问题。谢谢
我发现了我的错误,所以我自己回复了我的答案。
要注册模型以查看组件,注册对象必须是模型类型class。就我而言,我的 ListModelResourceManager 不是模型 class。所以需要使用 QStringListModel 作为模型 class 而不是 link 这个 QStringListModel 对象到 QML Combobox 组件。
我在这里为像我一样感到痛苦的人更新了我的工作源
ListModelResourceManager.hpp(管理QStringListMoel)
class ListModelResourceManager : public QObject {
Q_OBJECT
QStringListModel m_model;
public:
ListModelResourceManager(const QString& model_id, const QString& ctx_id, const QQmlApplicationEngine& engine, QObject* parent = nullptr);
};
ListModelResourceManager.cpp(m_model通过注册到本地资源管理器的回调函数更新)
ListModelResourceManager::ListModelResourceManager(const QString& model_id, const QString& ctx_id, const QQmlApplicationEngine& engine, QObject* parent)
: QObject(parent), m_model()
{
engine.rootContext()->setContextProperty(ctx_id, this);
engine.rootContext()->setContextProperty(model_id, &m_model);
rutResource::ResourceManager::getInstance().addResourceUpdatedListener([this](){
auto resources = rutResource::ResourceManager::getInstance().resources();
QStringList list;
for (auto item : resources)
{
list.append(QString::fromUtf8(item.first.c_str()));
}
m_model.setStringList(list);
});
}
main.cpp
int main(int argc, char *argv[])
{
// ...
ListModelResourceManager lmResourceManager("MODEL_Resources","ResourceManagerList", engine);
// ...
}
view.qml
// ...
ComboBox {
id : idResourceSelector
model : MODEL_Resources
textRole: "display"
}
// ...
ps。我想将我的本机资源管理器与 QT 界面分开,所以我使用回调侦听器。如果不是您的情况,您可以使用其他方法更新您的模型,例如 signal/slot 等...