簡體   English   中英

發送 qml 項目列表將 C++ 類連接到 QML :M16 錯誤

[英]send a list of qml Items connect C++ class to QML :M16 error

我將向我的 C++ 類發送一個 qml 項目列表,我已經使用了本教程https://rudigergad.com/2011/11/13/exchange-data-and-objects-between-c-and-qml反之亦然/

但是有一些問題。 首先讓我們看看我的代碼

地理參考對象_.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

主程序

#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但我想我已經根據該教程完成了所有事情,有人可以幫助我嗎? thnaks

問題是項目的名稱必須大寫,而在您的情況下不是,解決方案是例如:

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

和:

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

注意:這個

M16 錯誤 無效的屬性名稱

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM