国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

qml與C++交互傳值的簡單demo

2019-11-08 19:43:07
字體:
來源:轉載
供稿:網友

qml 與C++的后臺交互,參照foruok大神的寫法自己研究,改動了一下,有不足 的地方還望路過的大神指出,

我用的是Qt5.7.1,做了個簡單的demo,下面是我的代碼源碼

具體的操作過程如下:

新建選擇application項目 中的Qt Quick Controls 2 Application ,選擇下一步,命名為QmlTest后面一直點下一步就行了。

選擇項目右鍵選擇添加新文件,C++     C++ Class 下一步,命名為QmlTest,baseClass選擇QObject點擊下一步就OK了。

main.cpp的源碼

#include <QGuiApplication>#include <QQmlApplicationEngine>#include <QtQml>#include "qmltest.h"int main(int argc, char *argv[]){    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);    QGuiApplication app(argc, argv);    qmlRegisterType<QmlTest>("an.Qt.QmlTest",1,0,"QmlTest");    QQmlApplicationEngine engine;    engine.load(QUrl(QLatin1String("qrc:/main.qml")));    return app.exec();}注意上面的   qmlRegisterType<QmlTest>("an.Qt.QmlTest",1,0,"QmlTest");的位置不能放錯否則會沒有作用。

qmltest.h的源碼

#ifndef QMLTEST_H#define QMLTEST_H#include <QObject>class QmlTest : public QObject{    Q_OBJECTpublic:    explicit QmlTest(QObject *parent = 0);    ~QmlTest();signals:    void currentDemo(const QString &strDemo);public slots:    void send();};#endif // QMLTEST_Hqmltest.cpp的源碼

#include "qmltest.h"QmlTest::QmlTest(QObject *parent){}QmlTest::~QmlTest(){}void QmlTest::send(){    emit currentDemo(("demo"));}main.qml的源碼

import QtQuick 2.7import QtQuick.Controls 2.0import QtQuick.Layouts 1.0import an.Qt.QmlTest 1.0ApplicationWindow {    visible: true    width: 640    height: 480    title: qsTr("Hello World")    Text {        id: demoLabel;        anchors.left: parent.left;        anchors.leftMargin: 4;        anchors.top: parent.top;        anchors.topMargin: 4;        font.pixelSize: 26;    }    QmlTest {        id: demoMaker;    }    Button{        id:send;        text: "send";        anchors.left: parent.left;        anchors.leftMargin: 4;        anchors.bottom: parent.bottom;        anchors.bottomMargin: 4;        onClicked: {            demoMaker.send();        }    }    Connections {        target: demoMaker;        onCurrentDemo:{            demoLabel.text = strDemo;        }    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 二连浩特市| 舒城县| 健康| 上饶县| 望谟县| 五台县| 隆子县| 蒲江县| 宾阳县| 多伦县| 邵武市| 平远县| 丹巴县| 华阴市| 太原市| 时尚| 酒泉市| 巴青县| 昌图县| 利津县| 晋中市| 左贡县| 白山市| 武夷山市| 同心县| 穆棱市| 江安县| 张北县| 汶上县| 镇沅| 资兴市| 诸城市| 东阳市| 郓城县| 华容县| 新宁县| 尼木县| 衢州市| 铜鼓县| 错那县| 满洲里市|