问题描述
myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QDebug>
#include <QObject>
class MyClass : public QObject
{
public:
MyClass();
public slots:
void buttonClicked();
void buttonClicked(QString &in);
};
#endif // MYCLASS_H
myclass.cpp
#include "myclass.h"
MyClass::MyClass()
{
}
void MyClass::buttonClicked()
{
// Do Something
}
void MyClass::buttonClicked(QString &in)
{
qDebug() << in;
}
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <myclass.h>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
MyClass myClass; // A class containing my functions
// Trying to "SetContextProperty" as I saw people do it to achieve C++/QML connection
QQmlContext * context = new QQmlContext(engine.rootContext());
context->setContextProperty("_myClass", &myClass);
return app.exec();
}
我想在 myClass 类中使用一个函数,该函数在单击 QML 按钮时采用 QString 参数..
I want to use a function in myClass class which takes a QString parameter when a QML button is clicked ..
当我编译 &运行..一切顺利.但是当我点击按钮..它在调试器中显示这个错误:
When I compile & run .. it all goes smoothly.But when I click on the button .. it shows this error in the debugger:
qrc:///main.qml:80: ReferenceError: _myClass is not defined
~> 我的 QML 文件中的第 80 行":
~> "Line 80 in my QML file":
74: MouseArea {
75: id: mouseArea1
76: anchors.fill: parent
77: hoverEnabled: true;
78: onEntered: { rectangle1.border.width = 2 }
79: onExited: { rectangle1.border.width = 1 }
80: onClicked: _myClass.buttonClicked("Worked?")
81: }
(至于错误导致编译错误)
( As for the error caused the compiling error )
正如 @Jairo 所建议的,所有类都必须继承自 QObject.
As @Jairo suggested, all classes must be inherited from QObject.
仍在寻找解决我主要问题的方法.
Still looking for a solution to my main problem.
推荐答案
哦哦.这里有几个问题.(代码甚至可以编译吗?)
Uh oh. There's several things wrong here. (Does the code even compile?)
首先要做的事情.将某些内容传递给 QML 引擎的根属性时,您无法创建新上下文 - 您必须直接使用根上下文,如下所示:
First things first. When passing something to the QML engine's root property, you can't create a new context -- you have to use the root context directly, like so:
engine.rootContext()->setContextProperty("_myClass", &myClass);
接下来,类定义有一些问题.在下面的代码中查看我的评论:
Next, the class definition has some problems. See my comments in the code below:
class MyClass : public QObject
{
// This macro is required for QObject support. You should get a compiler
// error if you don't include this.
Q_OBJECT
public:
// QObjects are expected to support a parent/child hierarchy. I've modified
// the constructor to match the standard.
explicit MyClass(QObject *parent = 0);
public slots:
// This method needs to take either a QString or a const reference to one.
// (QML doesn't support returning values via the parameter list.)
void buttonClicked(const QString& in);
};
构造函数的实现应该将父类传递给基类:
The constructor's implementation should pass the parent on to the base class:
MyClass::MyClass(QObject *parent) :
QObject(parent)
{
}
这篇关于Qt - 单击 QML 按钮时如何运行 C++ 函数?使用QQmlApplicationEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!