QML TableView 模型悄无声息地失败了?
QML TableView Model Silently Failing?
编辑:添加了一些说明
使用 QML 5.14
似乎 TableView 的 model
属性不想显示 QList<int>
或 int
的任何变体,无论是 qint8
、qint32
,等等。我可以使用无符号 QList<uint>
,但是我需要在我的应用程序中保持负值范围。
我发现信息正在进入 qml 层,因为当我调用时:
console.log("cfs.amounts is " + cfs.amounts)
console.log("model is " + model)
console.log("modellength is " + model.length)
我实际上得到了预期的控制台输出:
qml: cfs.amounts is 11,12
qml: model is 11,12
qml: modellength is 2
我通过直接传递数据确保 TableView 正常工作,即 model: [11, 22]
并且它显示正确,即它显示索引 0, 1
.但是,当我将 cfs.amounts
传递给它时,我根本无法让它显示任何内容,这是 C++ 中的 QList<int>
。所以根据 console.log
,模型数据在那里,它是正确的,它从 c++ 传递到 qml 没有问题,而且长度很好——TableView 只是无法显示它。
我唯一能想到的是,TableView 默默地无法显示有符号整数数组。但是我也可能完全错了,因为我不能获取 Repeater
项目以在其 model
中识别它,两者都不是。我已经搜索过,但找不到关于此主题的任何错误报告。有没有人对如何让 qml 模型识别传递的 QList<int>
有任何建议?这一切都在 QML 5.14 中。
cashflowschedule.h
#ifndef CASHFLOWSCHEDULE_H
#define CASHFLOWSCHEDULE_H
#include "QObject"
#include "QList"
class CashFlowSchedule : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<int> amounts READ amounts)
public:
CashFlowSchedule() {};
QList<int> amounts() { return {11,12}; }
};
#endif // CASHFLOWSCHEDULE_H
main.qml
import QtQuick 2.14
import QtQuick.Window 2.14
import cpps 1.0
Window {
visible: true
width: 640
height: 480
CashFlowSchedule { id: cfs }
TableView {
anchors.fill: parent
model: cfs.amounts
delegate: Text { text: index }
Component.onCompleted: {
console.log("cfs.amounts is " + cfs.amounts)
console.log("model is " + model)
}
}
}
包含在main.cpp
#include "cashflowschedule.h"
...
qmlRegisterType<CashFlowSchedule>("cpps", 1, 0, "CashFlowSchedule");
...
QList<int>
不是用于模型的官方支持的 C++ 类型之一(请参阅列表 here). A bug report exists 以澄清关于这一点的文档。QVariantList 是一个很好的替代方法。
编辑:添加了一些说明
使用 QML 5.14
似乎 TableView 的 model
属性不想显示 QList<int>
或 int
的任何变体,无论是 qint8
、qint32
,等等。我可以使用无符号 QList<uint>
,但是我需要在我的应用程序中保持负值范围。
我发现信息正在进入 qml 层,因为当我调用时:
console.log("cfs.amounts is " + cfs.amounts)
console.log("model is " + model)
console.log("modellength is " + model.length)
我实际上得到了预期的控制台输出:
qml: cfs.amounts is 11,12
qml: model is 11,12
qml: modellength is 2
我通过直接传递数据确保 TableView 正常工作,即 model: [11, 22]
并且它显示正确,即它显示索引 0, 1
.但是,当我将 cfs.amounts
传递给它时,我根本无法让它显示任何内容,这是 C++ 中的 QList<int>
。所以根据 console.log
,模型数据在那里,它是正确的,它从 c++ 传递到 qml 没有问题,而且长度很好——TableView 只是无法显示它。
我唯一能想到的是,TableView 默默地无法显示有符号整数数组。但是我也可能完全错了,因为我不能获取 Repeater
项目以在其 model
中识别它,两者都不是。我已经搜索过,但找不到关于此主题的任何错误报告。有没有人对如何让 qml 模型识别传递的 QList<int>
有任何建议?这一切都在 QML 5.14 中。
cashflowschedule.h
#ifndef CASHFLOWSCHEDULE_H
#define CASHFLOWSCHEDULE_H
#include "QObject"
#include "QList"
class CashFlowSchedule : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<int> amounts READ amounts)
public:
CashFlowSchedule() {};
QList<int> amounts() { return {11,12}; }
};
#endif // CASHFLOWSCHEDULE_H
main.qml
import QtQuick 2.14
import QtQuick.Window 2.14
import cpps 1.0
Window {
visible: true
width: 640
height: 480
CashFlowSchedule { id: cfs }
TableView {
anchors.fill: parent
model: cfs.amounts
delegate: Text { text: index }
Component.onCompleted: {
console.log("cfs.amounts is " + cfs.amounts)
console.log("model is " + model)
}
}
}
包含在main.cpp
#include "cashflowschedule.h"
...
qmlRegisterType<CashFlowSchedule>("cpps", 1, 0, "CashFlowSchedule");
...
QList<int>
不是用于模型的官方支持的 C++ 类型之一(请参阅列表 here). A bug report exists 以澄清关于这一点的文档。QVariantList 是一个很好的替代方法。