C++ Qt:公开C++;类到QML

C++ Qt:公开C++;类到QML,c++,qt,qml,qproperty,C++,Qt,Qml,Qproperty,在运行应用程序时,我不断收到相同的错误: qrc:/main.qml:13:ReferenceError:\u未定义myClass 臭虫在哪里 此外,如果我想将信号myIntChanged连接到插槽,我应该将连接放在哪里?在main.cpp中还是在构造函数MyClass中 myclass.h #ifndef MYCLASS_H #define MYCLASS_H #include <QObject> #include <QString> #include <QDe

在运行应用程序时,我不断收到相同的错误:

qrc:/main.qml:13:ReferenceError:\u未定义myClass

臭虫在哪里

此外,如果我想将信号myIntChanged连接到插槽,我应该将连接放在哪里?在main.cpp中还是在构造函数MyClass中

myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QObject>
#include <QString>
#include <QDebug>
#include <QTimer>

class MyClass : public QObject
{
    Q_OBJECT

public:
    MyClass(QObject *parent = 0);

    Q_PROPERTY(int myInt READ getMyInt WRITE setMyInt NOTIFY myIntChanged)
    int myInt;
    inline int getMyInt() { return myInt; }
    inline void setMyInt(int _value) { myInt = _value; }

private:
    void initVariables();

    QTimer *timer;

private slots:
    void editVariables();

signals:
    void myIntChanged();
};

#endif // MYCLASS_H

导致此问题的原因是,
load()
语句正在加载.qml文件,当读取该文件并验证变量是否存在时,他们意识到此对象不存在,因为它是通过
setContextProperty()
传递给他们的,一种可能的解决方案是在加载.qml之前将对象传递给它:

int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    MyClass *myClass = new MyClass();
    engine.rootContext()->setContextProperty("_myClass", myClass);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
    return app.exec();
}
它不会更新,因为它没有通过您声明的信号得到通知。正确的做法是在每次属性更改时发出信号,您必须修改某些方法:

inline void setMyInt(int _value) {
    if(myInt == _value)
        return;
    myInt = _value;
    emit myIntChanged();
}

void MyClass::editVariables()
{
setMyInt(getMyInt()+1);

qDebug()你有没有试着从我的班级中淘汰出来myclass@Frrank已尝试,但没有更改!此外,qml文本始终显示“0”,这是初始值,但它不会显示任何更新,即使计时器正常工作!我使用了Qt 5.10.0,如果此信息可能有用,请使用以下信息代替文本:\ u myClass.myInt。只需尝试文本:myClass。myInt@Frrank已经尝试过,但没有改变任何东西…这次有什么错误是的!错误会以这种方式消失!!但是为什么QML文本不会在计时器超时时更新???@EyllanesPerfect,谢谢,你的答案非常清楚!-@eyllanesc
import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Exposed C++ Class")

    Text {
        anchors.top: parent.top; anchors.topMargin: 30
        anchors.left: parent.left; anchors.leftMargin: 30
        text: _myClass.myInt
    }
}
int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    MyClass *myClass = new MyClass();
    engine.rootContext()->setContextProperty("_myClass", myClass);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
    return app.exec();
}
inline void setMyInt(int _value) {
    if(myInt == _value)
        return;
    myInt = _value;
    emit myIntChanged();
}
void MyClass::editVariables()
{
    setMyInt(getMyInt()+1);
    qDebug() << "myclass.cpp: timeout: variables edited.";
}