久久久久久久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 ()?環形?)
主站蜘蛛池模板: 亚洲精品福利视频 | 99国产精品一区二区三区 | 中文字幕国产一区 | 国内精品免费久久久久软件老师 | 久久精品亚洲 | 欧美日韩亚洲一区二区 | 视频一区二区中文字幕日韩 | 激情国产在线 | 嫩草最新网址 | 91一区二区三区在线观看 | 九九热视频这里只有精品 | 国产毛片久久久 | 精品国产免费一区二区三区演员表 | 日韩av一区二区在线观看 | 国产精彩视频在线观看 | 色女人天堂 | 成人久久久| 蜜臀久久 | 国产一区二区久久久 | 免费亚洲一区二区 | av在线免费网站 | 日韩亚洲欧美综合 | www.久久 | 玖玖国产精品视频 | 免费看片在线播放 | 91.色| 日韩中文字幕一区二区 | 国产成人精品久久二区二区91 | 激情欧美日韩一区二区 | 亚洲成人av在线播放 | 色综合天天天天做夜夜夜夜做 | 99这里只有精品视频 | 高清国产午夜精品久久久久久 | 久久久久久国产精品免费 | 国产精品福利一区二区三区 | av免费网站在线观看 | 成人区精品一区二区婷婷 | 91精品国产91久久综合桃花 | 国产福利91精品一区二区三区 | 久久这里只有 | 亚洲精品中文字幕在线观看 |