发送 QML 项目列表C++类连接到 QML :M16 错误



我要向我的 c++ 类发送一个 qml 项目列表,我已经使用过本教程 https://ruedigergad.com/2011/11/13/exchange-data-and-objects-between-c-and-qml-and-vice-versa/

但是有一些问题。首先让我们看看我的代码

georefobject_.h

#ifndef GEOREFOBJECT_H
#define GEOREFOBJECT_H
#include "gcppoint_.h"
#include <qdebug.h>
class georefobject_ : public QObject
{
  Q_OBJECT
public:
  explicit georefobject_(QObject *parent = 0) : QObject(parent){}
  Q_INVOKABLE void doA(gcppoint_ *a){
    qDebug() << "doing A: " << a->test();
  }
//  Q_INVOKABLE void doC(C *c){
//    qDebug() << "doing C: " << c->a()->test();
//  }
  Q_INVOKABLE void doAlist(QVariantList vl){
    qDebug() << "Doing AList... ";
    for(int i = 0; i < vl.size(); i++){
      qDebug() << "vl.at(" << i << "): " << vl.at(i);
      // Get the actual object out of a QVariant.
      gcppoint_ *a = qobject_cast<gcppoint_ *>(vl.at(i).value<QObject *>());
      qDebug() << "Data from A" << i << ": " << a->test();
    }
  }
  Q_INVOKABLE void doVl(QVariantList vl){
    qDebug() << "QVariantList passed as parameter.nSize of vl: " << vl.size();
    for(int i = 0; i < vl.size(); i++){
      qDebug() << "vl.at(" << i << "): " << vl.at(i);
    }
  }
  Q_INVOKABLE gcppoint_* makeA(){
    gcppoint_* a = new gcppoint_();
    a->setTest("Another A");
    return a;
  }
  Q_INVOKABLE QVariantList makeAList(){
    QVariantList vl;
    gcppoint_* a1 = new gcppoint_();
    a1->setTest("newA1");
    vl.append(qVariantFromValue((QObject*)a1));
    gcppoint_* a2 = new gcppoint_();
    a2->setTest("newA2");
    vl.append(qVariantFromValue((QObject*)a2));
    return vl;
  }
};
#endif // GEOREFOBJECT_H

gcppoint_.h

#ifndef GCPPOINT_H
#define GCPPOINT_H
#include <QObject>
class gcppoint_ : public QObject
{
  Q_OBJECT
  Q_PROPERTY(QString test READ test WRITE setTest NOTIFY testChanged)
public:
  explicit gcppoint_(QObject *parent = 0) : QObject(parent){}
  QString test(){return myTest;}
  void setTest(QString t){
    myTest = t;
    testChanged(myTest);
  }
signals:
  void testChanged(QString t);
private:
  QString myTest;
};
#endif // GCPPOINT_H

主.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "gcppoint_.h"
#include "gcppointobject.h"
#include "georefobject_.h"
#include <qdebug.h>
#include <qquickitem.h>
#include <QQuickView>
#include <QObject>
#include "gcppointobject.h"
#include "gcppoint_.h"
#include "georefobject_.h"
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    //qmlRegisterType<gcppointobject>("gcppointobject", 1, 0, "gcppointobject");
    qmlRegisterType<georefobject_>("georefobject", 1, 0, "georefobject_");
    qmlRegisterType<gcppoint_>("gcppoint", 1, 0, "gcppoint_");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

和 main.qml

import QtQuick.Layouts 1.3
import QtQuick 2.6
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls 2.0 as C2
import QtQuick.Window 2.0
import QtLocation 5.6
import QtPositioning 5.6
import QtGraphicalEffects 1.0
import QtQuick.Dialogs 1.0
//import gcppointobject 1.0
import gcppoint 1.0
import georefobject 1.0
ApplicationWindow {
    gcppoint_{id: aa; test: "bar"}
   // gcppoint_{id: aaa; test: "blah"}
    georefobject{id: b}
}

我得到两个错误,主要错误是georefobject_{id: b}它说invalid property name M16但我认为我已经根据该教程完成了所有事情,有人可以帮我吗?噗嗤

问题是项目的名称必须大写,而在您的情况下并非如此,解决方案是例如:

qmlRegisterType<gcppoint_>("gcppoint", 1, 0, "Gcppoint_");
qmlRegisterType<gcppoint_>("georefobject", 1, 0, "Georefobject_");

和:

Gcppoint_{
    id: aa; test: "bar"
}
Georefobject_{id: b}

注意:请参阅以下内容:

M16 错误 属性名称无效

相关内容

  • 没有找到相关文章

最新更新