【问题标题】:Qt compiler error when trying to use QQmlListProperty尝试使用 QQmlListProperty 时出现 Qt 编译器错误
【发布时间】: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&lt;QObject&gt;(this, &amp;m_vurms); 上遇到编译器错误:

main.cpp:20: error: C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'QQmlListProperty<QObject>'

我还尝试用 int 的 QList 替换 Vurm 的 QList - 问题似乎在于 Qt 在 QQmlListProperty&lt;T&gt;(this, &amp;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&lt;int&gt; 也会发生错误
  • 那我建议你提供一个简化的例子,我们可以自己测试一下。

标签: c++ qt qml qlist qquickitem


【解决方案1】:

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

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

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

【讨论】:

  • codereview.qt-project.org/#/c/190372 用于文档中的不匹配。
  • +1 for 我还必须删除 const 限定符才能使其编译。这对我来说是个问题。感谢您的帮助!
猜你喜欢
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
相关资源
最近更新 更多