【发布时间】:2018-08-15 16:46:57
【问题描述】:
我在 c++ 类中有一个 QList<QString> 类型的 Q_PROPERTY,它没有在 QML 中显示。该类如下所示:
class FooView : public QQuickItem
{
Q_OBJECT;
Q_PROPERTY(QList<QString> myStrings READ myStrings NOTIFY myStringsChanged);
private:
QList<QString> m_strings;
public:
FooView(QQuickItem * parent) : QQuickItem(parent), m_strings() {
m_strings << "String one" << "String two";
}
QList<QString> myStrings() const {
return m_strings;
}
signals:
void myStringsChanged();
};
上面的类使用qmlRegisterType注册为QML类型。我尝试将该属性用作ListView 的模型,如下所示:
FooView {
id: 'foo'
ListView {
anchors.fill: parent
model: foo.myStrings
delegate: Text {
text: "Hi" // to be replaced with foo.myStrings[index]
}
}
}
你不能用QList<QString>'s 作为模型吗?我认为你可以,因为它被列为简单列表类型。
【问题讨论】:
-
m_strings()?在构造函数中。 -
m_string()覆盖你的m_strings << "String one" << "String two";所以你使用的是空列表 -
@Phiber
m_strings << "String one" << "String two";它在m_string()之后运行,尽管m_string()是不必要的,因为默认情况下列表为空