尝试使用 QQmlListProperty 时出现 Qt 编译器错误
Qt compiler error when trying to use QQmlListProperty
我正在尝试使用 QQmlListProperty 从 QQuickItem 中公开 QList - 并遵循以下文档:
- 具有对象列表类型的属性
- QQmlList属性类
一个简化的示例:
#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编写/编译,C++11在.pro文件中设置。我正在Windows 10上的Qt Creator 4.2.1中编译:使用MSVC 2015 64位进行编译。
我之前错过了这一点,但是您需要将引用作为第二个参数传递给构造函数,而不是指针:
QQmlListProperty<Vurm> GameEngine::vurms()
{
return QQmlListProperty<Vurm>(this, m_vurms);
}
我还必须删除 const
限定符才能让它编译,这是有道理的,因为 QQmlListProperty
的构造函数需要一个非常量指针。当您尝试删除它时,错误可能仍然存在,因为您仍在传递指针。
相关文章:
- 如何使用MinGW编译器和SSL支持静态编译Qt
- Qt中的包装连接方法隐藏了编译器所需的信息
- CMake + Qt,moc 编译失败,无法实现 QMetaObject 方法(编译器找不到基本 ui 对象的标头?
- 使用其他编译器编译QT代码
- 使用 MSVC 套件时,在 Qt 中正确选择编译器
- Qt Creator在linux mint 64位中找不到CMAKE_CXX_COMPILER编译器
- Qt 编译器错误:未在此范围内声明"emit"
- 尝试使用 QQmlListProperty 时出现 Qt 编译器错误
- MSVC 编译器在调试模式下构建 Qt 5.10 时崩溃
- 将 QT Creator 5.8 与 MSVC 2015 编译器配合使用
- Qt Creator,项目工具包中的编译器被忽略
- 如何从我的qt应用程序运行gcc编译器?
- QT离线安装程序和兼容的C 编译器安装
- 尝试使用VC++编译器构建Qt应用程序:"type_traits":没有这样的文件或目录
- QT 5.7将-STD = GNU 11添加到我的编译器标志,Clobbering -Std = C 14
- 如何将QT5和QT Creator 2.6.1与VS 2012中的编译器一起使用
- Qt 5测试版2,Qt Creator 2.6,MSVC错误:Qt Creaator需要一个编译器来构建.在工具包选项中
- 将 -lm 标志传递给 qt 编译器以摆脱"无法打开文件 'm.lib'
- Qt:编译器堆空间不足
- Qt - 编译器在我的主窗口上调用 setLayout() 时抱怨