简体   繁体   English

无法从 qml 调用 c++ 方法:“对象的属性不是函数”

[英]Cannot invoke c++ method from qml: "Property of object is not a function"

I am now trying to find a solution for hours and I absolutely have no clue what is wrong.我现在试图找到几个小时的解决方案,我完全不知道出了什么问题。

I am trying to submit a string from QML to a C++ method and I get the error: "qrc:/main.qml:16: TypeError: Property 'test' of object [object Object] is not a function"我试图将一个字符串从 QML 提交到 C++ 方法,但出现错误:“qrc:/main.qml:16: TypeError: Property 'test' of object [object Object] is not a function”

Has anyone an idea how to fix that?有谁知道如何解决这个问题?

Thank you very much!非常感谢!

Mabe related but I do not find a solution Mabe 相关,但我没有找到解决方案

main.cpp主程序

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "data.h"
#include <QQmlContext>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    qmlRegisterType<Data>("com.br.classes", 1, 0, "Data");

    Data* myData = new Data();

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("data", myData);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
 }

data.h数据.h

#ifndef DATA_H
#define DATA_H

#include <QObject>
#include <QDebug>

class Data : public QObject
{  
    Q_OBJECT
public:
    explicit Data(QObject *parent = nullptr);

    Q_INVOKABLE void test(QString strg) {qDebug() << "Received string: " << strg;}
};

#endif // DATA_H

main.qml主文件

import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import com.br.classes 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Button {
        text: "Press me!"
        onClicked: {
            data.test("test");
        }
    }
}

Window it has a property called data , so you should not use that name since it will be understood that the data refers to that attribute and not to the object that you are passing on to it. Window它有一个名为data的属性,因此您不应该使用该名称,因为可以理解数据是指该属性而不是您传递给它的对象。

The solution is to change the name:解决办法是改名:

C++ C++

engine.rootContext()->setContextProperty("helper", myData);

QML: QML:

helper.test("test");

On the other hand it is not necessary to register the type if you are going to use setContextProperty() .另一方面,如果您要使用setContextProperty() ,则不需要注册类型。

The issue is probably that you named the context property data , which is already a property of Item .问题可能是您将上下文属性data命名为已经是Item的属性。 Call the property something else and it should work.将该属性称为其他名称,它应该可以工作。

better you create an object of that class in QML code and then use its functions.最好在 QML 代码中创建该类的对象,然后使用其函数。

your main.qml你的 main.qml

import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import com.br.classes 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Data { id: data }

    Button {
        text: "Press me!"
        onClicked: {
            data.test("test");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM