c++ - 发送 qml 项目列表将 C++ 类连接到 QML :M16 error

标签 c++ qt qml qt5

我将向我的 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 但我想我已经根据那个做了所有事情教程, 有人可以帮我吗? 谢谢

最佳答案

问题是 Items 的名称必须大写,而在您的情况下不是,解决方案例如:

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

和:

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

注意:请参阅 this :

M16 Error Invalid property name name

关于c++ - 发送 qml 项目列表将 C++ 类连接到 QML :M16 error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46961271/

相关文章:

javascript - 你如何使用 JavaScript 在 QtQuick Qml 中进行大量数学运算

qt - 如何使用基于字段值选择的异构委托(delegate)制作 QtQuick TableView/TreeView

c++ - 谷歌模拟 : Mock private variable member that is instantiated in target class's constructor

c++ - fstream 不会打印到文件

android - 调用callback()到Java时应用崩溃。由于detatchThread而导致运行时错误

c++ - 连接到数据库时出现错误 `driver not loaded`

c++ - 带有 "subtree"的 QTreeWidgetItemIterator

c++ - QML ListView 从 mysql 数据库中检索数据

c++ - 使用 std::bind 时出现运行时错误

Qt - 将光标更改为沙漏并禁用光标