【发布时间】:2017-04-02 01:01:57
【问题描述】:
我正在尝试使用 QQmlListProperty 从 QQuickItem 中公开 QList - 并遵循以下文档:
一个简化的例子:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickItem>
#include <QList>
#include <QQmlListProperty>
class GameEngine : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<QObject> vurms READ vurms)
public:
explicit GameEngine(QQuickItem *parent = 0) :
QQuickItem(parent)
{
}
QQmlListProperty<QObject> vurms() const
{
return QQmlListProperty<QObject>(this, &m_vurms);
}
protected:
QList<QObject*> m_vurms;
};
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
return app.exec();
}
#include "main.moc"
但我在return QQmlListProperty<QObject>(this, &m_vurms); 上遇到编译器错误:
main.cpp:20: error: C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'QQmlListProperty<QObject>'
我还尝试用 int 的 QList 替换 Vurm 的 QList - 问题似乎在于 Qt 在 QQmlListProperty<T>(this, &m_vurms); 中所做的任何事情
我正在使用 Qt 5.8 编写/编译,并且在 .pro 文件中设置了 C++11。我正在 Windows 10 上的 Qt Creator 4.2.1 中编译:使用 MSVC 2015 64 位进行编译。
【问题讨论】:
-
尝试从函数中删除
const限定符。 -
@Mitch - 即使不使用限定符,错误仍然存在,并且当我阅读文档时,const 需要在那里......
-
我不确定文档中发生了什么......它在声明中是 const ,但不是在定义中。也许问题出在
Vurm- 你能提供头文件吗? -
@Mitch - 是的,在 Vurm 类中什么都不是 - 即使使用
QList<int>也会发生错误 -
那我建议你提供一个简化的例子,我们可以自己测试一下。
标签: c++ qt qml qlist qquickitem