从QML访问C ++函数 [英] Access C++ function from QML

查看:160
本文介绍了从QML访问C ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Qt做一个小程序。我有一个 main.cpp 以下代码:

  #include < QtGui / QApplication的> 
#includeqmlapplicationviewer.h

Q_DECL_EXPORT int main(int argc,char * argv [])
{
QScopedPointer< QApplication> app(createApplication(argc,argv));

QmlApplicationViewer查看器;
viewer.setOrientation(QmlApplicationViewer :: ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String(qml / tw_looptijden_berekenen / main.qml));
viewer.showExpanded();

return app-> exec();
}

int reken_tijden_uit(){
return true;
}

我有一个 .qml $ b

  import QtQuick 1.1 

矩形{

width:360
height:360
Text {
text:qsTr(Hello World)
anchors.centerIn:parent
}
MouseArea {
anchors.fill:parent
onClicked:{
Qt.quit();
}
}
}

现在,当我点击 MouseArea ,程序退出。我想要的是它在 main.cpp 文件中调用函数 reken_tijden_uit



我搜索了很多,并在本网站上搜索到。我发现了一些答案,但我没有得到一个工作。



那么我要放哪些代码,以便我可以调用函数 reken_tijden_uit in C ++?



预先感谢。 b

头文件如下所示:

  #ifndef EIGEN_FUNCTION_HEADER_H 
#define EIGEN_FUNCTION_HEADER_H

class MyObject:public QObject {
Q_OBJECT $ b $ public:
显式MyObject(QObject * parent = 0):QObject(parent){}
Q_INVOKABLE int reken_tijden_uit(){
return 1;
}
};

#endif // EIGEN_FUNCTION_HEADER_H

main。 cpp

  #include< QtGui / QApplication> 
#includeqmlapplicationviewer.h
#includeeigen_function_header.h

QScopedPointer< QApplication> app(createApplication(argc,argv));

qmlRegisterType< MyObject>(com.myself,1,0,MyObject);

Q_DECL_EXPORT int main(int argc,char * argv [])
{
QScopedPointer< QApplication> app(createApplication(argc,argv));

QmlApplicationViewer查看器;
viewer.setOrientation(QmlApplicationViewer :: ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String(qml / tw_looptijden_berekenen / main.qml));
viewer.showExpanded();

return app-> exec();
}

和QML文件:

  import QtQuick 1.1 
import com.myself 1.0

矩形{
宽度:360
高度:360
Text {
text:qsTr(Hello World)
anchors.centerIn:parent
}
MyObject {
id:myobject
}
MouseArea {
anchors.fill:parent
onClicked:{
myobject.reken_tijden_uit()
}
}
}

错误如下:

  D:\ * \ main.cpp:6:错误:'argc'未在此范围内声明
D:\ * \ main.cpp:6:error:' argv'未在此范围内声明
D:\ * \ main.cpp:8:错误:预期的构造函数,析构函数或'<'令牌之前的类型转换

对于任何C + +代码从QML调用,它必须驻留在 QObject 中。



你需要做的是创建使用你的函数一个 QObject 后代类,把它注册到QML,在你的QML中实例化它并调用函数。
还要注意你必须用 Q_INVOKABLE 来标记你的函数。



代码:


  #ifndef EIGEN_FUNCTION_HEADER_H 
#define EIGEN_FUNCTION_HEADER_H

#include< QObject>

class MyObject:public QObject {
Q_OBJECT $ b $ public:
explicit MyObject(QObject * parent = 0):QObject(parent){}
Q_INVOKABLE int reken_tijden_uit(){
return 1;
}
};

#endif // EIGEN_FUNCTION_HEADER_H

main.cpp:

  #include< QtGui / QApplication> 
#include< QtDeclarative>

#includeqmlapplicationviewer.h
#includeeigen_function_header.h

Q_DECL_EXPORT int main(int argc,char * argv [])
{
QScopedPointer< QApplication> app(createApplication(argc,argv));
qmlRegisterType< MyObject>(com.myself,1,0,MyObject);

QmlApplicationViewer查看器;
viewer.setOrientation(QmlApplicationViewer :: ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String(qml / tw_looptijden_berekenen / main.qml));
viewer.showExpanded();

return app-> exec();
}

QML:

  import QtQuick 1.1 
import com.myself 1.0

Rectangle {

width:360
height:360
Text {
text:qsTr(Hello World)
anchors.centerIn:parent
}
MyObject {
id:myobject
}
$ b MouseArea {
anchors.fill:parent
onClicked:{
console.log(myobject.reken_tijden_uit())
}
}
}


I'm trying to make a little program with Qt. I have a main.cpp with the following code:

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/tw_looptijden_berekenen/main.qml"));
    viewer.showExpanded();

    return app->exec();
}

int reken_tijden_uit(){
    return true;
}

and I have a .qml file:

import QtQuick 1.1

Rectangle {

width: 360
height: 360
Text {
    text: qsTr("Hello World")
    anchors.centerIn: parent
}
MouseArea {
    anchors.fill: parent
    onClicked: {
        Qt.quit();
    }
}
}

Now, when I click on the MouseArea, the program quits. What I want is that it calls the function reken_tijden_uit in the main.cpp file.

I've googled a lot, and searched on this site to. I've found a couple of answers, but I didn't get one working.

So what code do I put where so I can call the function reken_tijden_uit in C++?

Thanks in advance.


The header file looks like this:

#ifndef EIGEN_FUNCTION_HEADER_H
#define EIGEN_FUNCTION_HEADER_H

class MyObject : public QObject{
   Q_OBJECT
public:
    explicit MyObject (QObject* parent = 0) : QObject(parent) {}
    Q_INVOKABLE int reken_tijden_uit(){
    return 1;
    }
};

#endif // EIGEN_FUNCTION_HEADER_H

main.cpp:

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "eigen_function_header.h"

QScopedPointer<QApplication> app(createApplication(argc, argv));

qmlRegisterType<MyObject>("com.myself", 1, 0, "MyObject");

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/tw_looptijden_berekenen/main.qml"));
    viewer.showExpanded();

    return app->exec();
}

and the QML file:

import QtQuick 1.1
import com.myself 1.0

Rectangle {
    width: 360
    height: 360
    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }
    MyObject {
        id: myobject
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            myobject.reken_tijden_uit()
        }
    }
}

And the errors are as follow:

D:\*\main.cpp:6: error: 'argc' was not declared in this scope
D:\*\main.cpp:6: error: 'argv' was not declared in this scope
D:\*\main.cpp:8: error: expected constructor, destructor, or type conversion before '<' token

So what did I do wrong?

解决方案

For any C++ code to be called from QML, it must reside inside a QObject.

What you need to do is create a QObject descended class with your function, register it to QML, instantiate it in your QML and call the function. Note also that you have to mark your function with Q_INVOKABLE.

Code:

#ifndef EIGEN_FUNCTION_HEADER_H
#define EIGEN_FUNCTION_HEADER_H

#include <QObject>

class MyObject : public QObject{
   Q_OBJECT
public:
    explicit MyObject (QObject* parent = 0) : QObject(parent) {}
    Q_INVOKABLE int reken_tijden_uit(){
    return 1;
    }
};

#endif // EIGEN_FUNCTION_HEADER_H

main.cpp:

#include <QtGui/QApplication>
#include <QtDeclarative>

#include "qmlapplicationviewer.h"
#include "eigen_function_header.h"

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));
    qmlRegisterType<MyObject>("com.myself", 1, 0, "MyObject");

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/tw_looptijden_berekenen/main.qml"));
    viewer.showExpanded();

    return app->exec();
}

QML:

import QtQuick 1.1
import com.myself 1.0

Rectangle {

    width: 360
    height: 360
    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }
    MyObject {
       id: myobject
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            console.log(myobject.reken_tijden_uit())
        }
    }
}

这篇关于从QML访问C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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