久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 QML 訪問 C++ 函數

Access C++ function from QML(從 QML 訪問 C++ 函數)
本文介紹了從 QML 訪問 C++ 函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試用 Qt 制作一個小程序.我有一個帶有以下代碼的 main.cpp:

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

我有一個 .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.

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.

那么我應該把什么代碼放在哪里,以便我可以在 C++ 中調用函數 reken_tijden_uit?

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

提前致謝.

頭文件如下所示:

#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 中.

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

您需要做的是使用您的函數創建一個 QObject 后代類,將其注冊到 QML,在您的 QML 中實例化它并調用該函數.另請注意,您必須使用 Q_INVOKABLE 標記您的函數.

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.

代碼:

#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++ 函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: h视频免费观看 | 色就是色欧美 | 91美女在线 | 天堂精品视频 | 亚洲视频在线免费 | 99精品欧美一区二区三区综合在线 | 好好的日在线视频 | 日韩人体视频 | 中文字幕日韩欧美一区二区三区 | 婷婷久| 国产一区精品 | 懂色一区二区三区免费观看 | 一区二区中文字幕 | 成人免费视频观看 | 久久久无码精品亚洲日韩按摩 | 久久国产精品一区二区三区 | 精品久久不卡 | 一道本不卡视频 | 精品久久香蕉国产线看观看亚洲 | 中文字幕在线视频网站 | 久久久久久免费毛片精品 | 午夜寂寞影院在线观看 | h视频网站在线观看 | 国产乱一区二区三区视频 | 欧美欧美欧美 | 青草福利| 精品国产乱码久久久久久果冻传媒 | 91精品在线播放 | 99资源站| 亚洲97| 国产成人精品久久 | 国产黄色大片在线免费观看 | 精品久久久久久久久久久久久久 | 国产精品久久久久久久久久久免费看 | 99精品99| 国产精品一区在线观看你懂的 | 欧美成人h版在线观看 | 国产精品夜夜夜一区二区三区尤 | 九九热精品免费 | 91精品久久久久久久 | 久热精品在线播放 |