QQmlListProperty:无法分配给不存在的属性“lon”lon:“3”

时间:2013-11-12 12:51:22

标签: qt qml qt-quick

我想知道如何使用 QQmlListProperty 。我尝试了以下但我不确定这是否是正确的方法。我在标题中显示错误。

aa.h

#ifndef IMO
#define IMO

#include <QQmlListProperty>
#include "DummyClass.h"

class SubClass: public QObject
{
  Q_OBJECT

  Q_PROPERTY (QQmlListProperty <DummyClass> functionWhat READ functionWhat)

public:
  SubClass (QObject *parent = 0);
  QQmlListProperty <DummyClass> functionWhat()
  {
    return QQmlListProperty <DummyClass> (this, dummyList);
  }

private:
  QList <DummyClass*> dummyList;   
};

#endif

aa.cpp

#include "aa.h"

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"

SubClass :: SubClass (QObject *parent) : QObject (parent)
{
  qDebug ("In Constructor.");
}

void appendDummies (QQmlListProperty<DummyClass> *property, DummyClass *dc)
{
  qDebug ("Do nothing. Can't add to a directory using this method.");
}

DummyClass.h

#ifndef IM
#define IM
#include <QObject>

class DummyClass : public QObject
{
  private:
    Q_OBJECT
    Q_PROPERTY (bool functionWhat READ functionWhat WRITE functionSetWhat)  
  public:
    DummyClass (QObject *parent = 0) {}

    float lat; float lon;

    bool functionWhat ()
    {
        qDebug ("In functionWhat");
    }

  public slots:
    void functionSetWhat (bool arg)
    {
        qDebug ("In functionWhatSlot");
    }

  signals:
    void functionWhatChanged (bool arg);
};

#endif

的main.cpp

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "/home/anisha/qmllistproperties/DummyClass.h"
#include <QtQml>
#include "/home/anisha/qmllistproperties/aa.h"
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    const char* ocuui = "OCUUI"; // @uri OCUUI

    qmlRegisterType <DummyClass> (ocuui, 1, 0, "DummyClass");
    qmlRegisterType <SubClass> (ocuui, 1, 0, "SubClass");


    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/untitled/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

main.qml

import QtQuick 2.0
import OCUUI 1.0

Rectangle {
    width: 360
    height: 360
    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }

    SubClass
    {
       functionWhat:
       [
           DummyClass
           {
              lat: "2"
              lon: "3"
           }
       ]
    }


    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您似乎错过了latlonDummyClass属性的属性声明:

Q_PROPERTY (int lat READ getLat WRITE setLat)
Q_PROPERTY (int lon READ getLon WRITE setLon)

你应该学习this example