我已经开始学习Qt 5并且长时间陷入困境。
我有两节课。 ScoreHandler处理ScoreRecords列表。我用C ++创建了这两个类,并将ScoreHandler的一个实例设置为上下文属性。现在在QML中我可以分配模型,但委托无法看到ScoreRecord的属性。我必须在某处注册某些东西吗?请帮帮我。
scorerecord.h
class ScoreRecord : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString date READ date WRITE setDate NOTIFY dateChanged)
Q_PROPERTY(QString score READ score WRITE setScore NOTIFY scoreChanged)
public:
ScoreRecord(QObject *parent = 0);
ScoreRecord(const QString& n, const QString &d, const QString &s, QObject *parent = 0);
QString name() const;
void setName(const QString &str);
QString date() const;
void setDate(const QString &str);
QString score() const;
void setScore(const QString &str);
signals:
void nameChanged();
void dateChanged();
void scoreChanged();
public slots:
private:
QString m_name;
QString m_date;
QString m_score;
};
scorehandler.h
class ScoreHandler : public QObject
{
Q_OBJECT
private:
const char* SCORE_TABLE_FILENAME;
struct scoreRow {
char name[128];
char date[32];
char score[16];
};
public:
explicit ScoreHandler(QObject *parent = 0);
QList<ScoreRecord *> scoreList;
signals:
public slots:
void SaveScore(const QString &name, const QString &date, const QString &score);
void LoadScore();
};
的main.c
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
ScoreHandler* scoreHandler = new ScoreHandler();
QQmlContext* ctx = viewer.rootContext();
ctx->setContextProperty("MyScoreModel", QVariant::fromValue(scoreHandler->scoreList));
viewer.setMainQmlFile(QStringLiteral("qml/qmlListView/main.qml"));
viewer.showExpanded();
return app.exec();
}
QML文件
import QtQuick 2.0
Rectangle {
width: 360
height: 360
ListView {
width: 100; height: 100
anchors.fill: parent
model: MyScoreModel
delegate: Text {
text: name
}
}
}
请有人解释一下,为什么以下代码有效?是什么原因?
的main.c
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
ScoreHandler* scoreHandler = new ScoreHandler();
QList<QObject *> scoreList;
scoreList.append(new ScoreRecord("Jmeno1", "datum1", "score1"));
scoreList.append(new ScoreRecord("Jmeno2", "datum2", "score2"));
scoreList.append(new ScoreRecord("Jmeno3", "datum3", "score3"));
QQmlContext* ctx = viewer.rootContext();
ctx->setContextProperty("MyScoreModel", QVariant::fromValue(scoreList));
viewer.setMainQmlFile(QStringLiteral("qml/qmlListView/main.qml"));
viewer.showExpanded();
return app.exec();
}
答案 0 :(得分:3)
我相信有一种更清洁的方式。您需要在ScoreHandler上定义属性scoreList:
Q_PROPERTY(QQmlListProperty<ScoreRecord> scoreList READ scoreList)
一个包含ScoreRecord指针的私有变量:
QList _scoreRecords;
然后你需要实现访问器方法:
// ScoreHandler.h
QQmlListProperty<ScoreRecord> scoreList();
// ScoreHandler.cpp
QQmlListProperty<ScoreRecord> ScoreHandler::scoreList()
{
return QQmlListProperty<ScoreRecord>(this, _scoreRecords);
}
然后您可以从qml访问它。
QtCreator就此有一个例子。它被称为Extending QML - Object and List Property Types Example
答案 1 :(得分:0)
我坚信
ctx->setContextProperty("MyScoreModel", QVariant::fromValue(scoreHandler->scoreList));
执行当前列表的副本,在给出的示例中为空,并且在您修改scoreList
时不会更新。 Qt doc说:
注意:视图无法知道a的内容 QList已经改变。如果QList发生变化,则需要重置 通过再次调用QQmlContext :: setContextProperty()模型。
如果您需要真实的数据模型,请从QAbstractListModel
派生并实施必要的覆盖:data(...)
,rowCount()
和count
属性,roleNames()
。
最后一个示例有效,因为它将静态列表分配给上下文变量。与JavaScript相同:
[{name: Jmeno1, date: datum1, score: score1},
{name: Jmeno2, date: datum3, score: score2},
{name: Jmeno3, date: datum3, score: score3}]
答案 2 :(得分:0)
Nuno Santos回答很好,我还指出了你可能需要的两个重要修改:
请注意具有QML类型系统的ScoreRecord must be registered,例如:qmlRegisterType<ScoreRecord>("com.mycompany.database", 1, 0, "ScoreRecord");
如果ScoreRecord在命名空间中,you have to定义如下属性:
Q_PROPERTY(QQmlListProperty<myNamespace::ScoreRecord> scoreList READ scoreList)
答案 3 :(得分:0)
请注意,如果ScoreRecord
在命名空间中,则可能必须这样声明Q_PROPERTY。
Q_PROPERTY(QQmlListProperty<myNamespace::ScoreRecord> scoreList READ scoreList)