我是 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();
};
主文件
...
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”])时效果很好。
请让我知道我的代码部分有什么问题。谢谢