Qt - 当点击QML按钮时如何运行C ++功能?使用QQmlApplicationEngine [英] Qt - How to run a C++ function when QML button is clicked? Using QQmlApplicationEngine

查看:1823
本文介绍了Qt - 当点击QML按钮时如何运行C ++功能?使用QQmlApplicationEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

myclass.h



  #ifndef MYCLASS_H 
#define MYCLASS_H

#include< QDebug>
#include< QObject>

class MyClass:public QObject
{
public:
MyClass();

公共位置:
void buttonClicked();
void buttonClicked(QString& in);
};

#endif // MYCLASS_H






myclass.cpp

  #includemyclass.h

MyClass :: MyClass()
{
}

void MyClass :: buttonClicked()
{
//做某事
}

void MyClass :: buttonClicked(QString& in)
{
qDebug()<在;
}






cpp

  #include< QApplication> 
#include< QQmlApplicationEngine>
#include< myclass.h>
#include< QQmlContext>

int main(int argc,char * argv [])
{
QApplication app(argc,argv);

QQmlApplicationEngine引擎;
engine.load(QUrl(QStringLiteral(qrc:///main.qml)));

MyClass myClass; //一个包含我的函数的类

//我试图SetContextProperty,我看到人们做到这一点实现C ++ / QML连接
QQmlContext * context = new QQmlContext(engine.rootContext );
context-> setContextProperty(_ myClass,& myClass);

return app.exec();
}

我想使用 myClass 类中的函数当点击QML按钮时,它接受一个QString参数。



当我编译&运行一切顺利。
但是当我点击按钮..它显示在调试器中的这个错误:


qrc:/// main。 qml:80:ReferenceError:_myClass未定义


〜>我的QML文件中的行80:

  74:MouseArea {
75:id:mouseArea1
76:anchors.fill:parent
77:hoverEnabled:真正;
78:onEntered:{rectangle1.border.width = 2}
79:onExited:{rectangle1.border.width = 1}
80:onClicked:_myClass.buttonClicked(Worked? )
81:}






EDIT:(至于导致编译错误的错误)



建议使用 @Jairo ,所有类都必须继承QObject。



仍在寻找解决我的主要问题的方法。

解决方案

噢哦。这里有几个问题。 (代码是否甚至编译?)



首先。当传递一些东西到QML引擎的根属性,你不能创建一个新的上下文 - 你必须直接使用根上下文,如:

  engine.rootContext() - > setContextProperty(_ myClass,& myClass); 

接下来,类定义有一些问题。请在下面的代码中查看我的意见:

  class MyClass:public QObject 
{
//此宏是QObject支持所必需的。你应该得到一个编译器
//错误如果你不包括这个。
Q_OBJECT

public:
// QObject需要支持父/子层次结构。我修改了
//的构造函数来匹配标准。
explicit MyClass(QObject * parent = 0);

public slots:
//这个方法需要一个QString或const引用。
//(QML不支持通过参数列表返回值)
void buttonClicked(const QString& in);
};

构造函数的实现应将parent传递给基类:



MyClass :: MyClass(QObject * parent):
QObject(parent)
{
}


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();
}

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

~> "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:    }


EDIT: ( As for the error caused the compiling error )

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?)

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆