c++ - 从 QML 访问 C++ 函数

标签 c++ qt function qml

我正在尝试用 Qt 制作一个小程序。我有一个带有以下代码的 main.cpp:

#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;
}

我有一个 .qml 文件:

import QtQuick 1.1

Rectangle {

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

我用谷歌搜索了很多,并在这个网站上搜索到。我找到了几个答案,但没有一个能奏效。

那么我应该在哪里放什么代码,以便在 C++ 中调用函数 reken_tijden_uit

提前致谢。


头文件如下所示:

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

和 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: {
            myobject.reken_tijden_uit()
        }
    }
}

错误如下:

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

那么我做错了什么?

最佳答案

对于要从 QML 调用的任何 C++ 代码,它必须位于 QObject 中。

您需要做的是使用您的函数创建一个 QObject 派生类,将其注册到 QML,在您的 QML 中实例化它并调用该函数。 另请注意,您必须使用 Q_INVOKABLE 标记您的函数。

代码:

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

关于c++ - 从 QML 访问 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9500280/

相关文章:

c++ - OpenCV 中 VideoCapture 对象的状态

c++ - Qt, "convert project to qmake generated project"在Visual Studio中做什么

C - 标准库中函数的源代码

c - 在函数调用期间避免 sizeof 样板的宏函数

c++ - 是否可以将复制粘贴的代码跟踪到从中检索到的网站?

c++ - 蓝牙可以接收数据但不能发送数据(C++ socket编程通信Matlab)

c# - Win32 是否允许同时使用控制台和窗口的应用程序?

c++ - 多态QSharedPointer

c++ - 从 Qt Linux 应用程序连接到 MS SQLServer

javascript - 我怎样才能得到一个函数的名称?