c++ - 尝试使用 QQmlListProperty 时出现 Qt 编译器错误

标签 c++ qt qml qlist qquickitem

我正在尝试使用 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 位进行编译。

最佳答案

我之前错过了这个,但是你需要将引用作为第二个参数传递给构造函数,而不是指针:

QQmlListProperty<Vurm> GameEngine::vurms()
{
    return QQmlListProperty<Vurm>(this, m_vurms);
}

我还必须删除 const 限定符才能使其编译,这是有道理的,因为 QQmlListProperty 的构造函数需要一个非常量指针。当您尝试删除它时错误可能仍然存在,因为您仍在传递指针。

关于c++ - 尝试使用 QQmlListProperty 时出现 Qt 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163602/

相关文章:

c++ - App Store 是否允许带有嵌入式 dylib 的 iOS 8 应用程序?

qt - QML 中的字典

c++ - 链接 : fatal error LNK1181: cannot open input file 'libclamav.lib'

c++ - 在 C++ 中标记类/方法已过时或弃用

c++ - 在文件中维护链表

c++ - 如何在 C++ 中从头开始反序列化文件(没有库)

c++ - QDataStream自定义写法——一行vs多行;

Qt - 如何在 QComboBox 中使用富文本?

qml - QML 布局中元素的自动换行

c++ - 如何将带有自定义对象的容器从 C++ 传递到 QML?