当前位置: 首页 > news >正文

广东佛山顺德疫情最新情况需要优化的网站有哪些?

广东佛山顺德疫情最新情况,需要优化的网站有哪些?,乐都企业网站建设哪家快,企业网站建立庆云县有几家目录 1 QML获取C的变量值 2 QML获取C创建的自定义对象 3 QML发送信号绑定C端的槽 4 C端发送信号绑定qml端槽 5 C调用QML端函数 1 QML获取C的变量值 QQmlApplicationEngine engine; 全局对象 上下文属性 QQmlApplicationEngine engine; QQmlContext *context1 engine.…

目录

1 QML获取C++的变量值

2 QML获取C++创建的自定义对象

3 QML发送信号绑定C++端的槽

4 C++端发送信号绑定qml端槽

5 C++调用QML端函数


1 QML获取C++的变量值
QQmlApplicationEngine engine;

全局对象

上下文属性

QQmlApplicationEngine engine;
QQmlContext *context1 = engine.rootContext();
context1->setContextProperty("test",200);

在qml中可全局直接使用test 

2 QML获取C++创建的自定义对象

光标放在成员变量m_iValue和m_sString后面 Alt + Enter 选择第一个可自动生成函数

int m_iValue;
QString m_sString;

myobject.h

#ifndef MYOBJECT_H
#define MYOBJECT_H#include <QObject>class MyObject : public QObject
{Q_OBJECT
public:explicit MyObject(QObject *parent = nullptr);int iValue() const;void setIValue(int newIValue);const QString &sString() const;void setSString(const QString &newSString);signals:void iValueChanged();void sStringChanged();private:int m_iValue;QString m_sString;Q_PROPERTY(int iValue READ iValue WRITE setIValue NOTIFY iValueChanged)Q_PROPERTY(QString sString READ sString WRITE setSString NOTIFY sStringChanged)
};#endif // MYOBJECT_H

myobject.c

#include "myobject.h"MyObject::MyObject(QObject *parent): QObject{parent}
{}int MyObject::iValue() const
{return m_iValue;
}void MyObject::setIValue(int newIValue)
{if (m_iValue == newIValue)return;m_iValue = newIValue;emit iValueChanged();
}const QString &MyObject::sString() const
{return m_sString;
}void MyObject::setSString(const QString &newSString)
{if (m_sString == newSString)return;m_sString = newSString;emit sStringChanged();
}

mian.c

注册main.qml 中 import testObj 1.0使用

qmlRegisterType<MyObject>("testObj",1,0,"MyObject");

main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {width: 640height: 480visible: truetitle: qsTr("Hello World")MyObject {iValue: 20sString: "test"}}

使用c++端的函数时需要在函数前面加上 Q_INVOKABLE

比如 Q_INVOKABLE void myFunction();

3 QML发送信号绑定C++端的槽

在mian.c先注册 qmlRegisterType<MyObject>("testObj",1,0,"MyObject");

C++ 写一个公有槽 

public slots:
    void qml_slot(QString str);

qml端通过按钮发送信号

signal qml_signal(string str)
    Button {
        onClicked: {
            qml_signal("qml send signal test");
        }
    }

绑定信号和槽函数

Component.onCompleted: {
        qml_signal.connect(myobj.qml_slot)
    }

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {id: windowwidth: 640height: 480visible: truetitle: qsTr("Hello World")MyObject {id: myobjiValue: 20sString: "test"}signal qml_signal(string str)Button {onClicked: {qml_signal("qml send signal test");}}//方法一
//    Connections {
//        target: window
//        function onQml_signal(str){
//            myobj.qml_slot(str)
//        }
//    }//方法二Component.onCompleted: {qml_signal.connect(myobj.qml_slot)}//方法三 mian.c 中engine load后//auto obj_list = engine.rootObjects();//auto window = list.first();//connect(window,SIGNAL(qml_signal(QString)),your function ptr,SLOT(qml_slot(QString)));}
4 C++端发送信号绑定qml端槽

myobject.h

信号 void signal_Cpp(QString str); 通过函数

Q_INVOKABLE void myFunction();发送
#ifndef MYOBJECT_H
#define MYOBJECT_H#include <QObject>class MyObject : public QObject
{Q_OBJECT
public:explicit MyObject(QObject *parent = nullptr);int iValue() const;void setIValue(int newIValue);const QString &sString() const;void setSString(const QString &newSString);Q_INVOKABLE void myFunction();static MyObject* getInstance();
public slots:void qml_slot(QString str);
signals:void iValueChanged();void sStringChanged();void signal_Cpp(QString str);
private:int m_iValue;QString m_sString;Q_PROPERTY(int iValue READ iValue WRITE setIValue NOTIFY iValueChanged)Q_PROPERTY(QString sString READ sString WRITE setSString NOTIFY sStringChanged)
};#endif // MYOBJECT_H

myobject.c

#include "myobject.h"
#include <QDebug>
MyObject::MyObject(QObject *parent): QObject{parent}
{}int MyObject::iValue() const
{return m_iValue;
}void MyObject::setIValue(int newIValue)
{if (m_iValue == newIValue)return;m_iValue = newIValue;emit iValueChanged();
}const QString &MyObject::sString() const
{return m_sString;
}void MyObject::setSString(const QString &newSString)
{if (m_sString == newSString)return;m_sString = newSString;emit sStringChanged();
}void MyObject::myFunction()
{emit signal_Cpp("signal_Cpp myFunction!");
}MyObject *MyObject::getInstance()
{static MyObject* obj = new MyObject();return obj;
}void MyObject::qml_slot(QString str)
{qDebug()<<"qml_slot"<<str;
}

mian.c

注册单例类

qmlRegisterSingletonInstance("testObj",1,0,"MyObject",MyObject::getInstance());
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtQml/qqml.h>
#include "myobject.h"int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endifQGuiApplication app(argc, argv);QQmlApplicationEngine engine;QQmlContext *context1 = engine.rootContext();context1->setContextProperty("test",200);//qmlRegisterType<MyObject>("testObj",1,0,"MyObject");qmlRegisterSingletonInstance("testObj",1,0,"MyObject",MyObject::getInstance());const QUrl url(QStringLiteral("qrc:/main.qml"));QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,&app, [url](QObject *obj, const QUrl &objUrl) {if (!obj && url == objUrl)QCoreApplication::exit(-1);}, Qt::QueuedConnection);engine.load(url);return app.exec();
}

main.qml

C++端发送信号

Button {
        onClicked: {
            //C++发送信号
            MyObject.myFunction()
        }
    }

连接QML端槽函数

Connections {
        target: MyObject
        function onSignal_Cpp(str) {
            //qml槽函数
            qmlSolt(str)
        }
    }

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {id: windowwidth: 640height: 480visible: truetitle: qsTr("Hello World")//    MyObject {
//        id: myobj
//        iValue: 20
//        sString: "test"//    }function qmlSolt(str) {console.log(str)}signal qml_signal(string str)Button {onClicked: {//C++发送信号MyObject.myFunction()}}Connections {target: MyObjectfunction onSignal_Cpp(str) {//qml槽函数qmlSolt(str)}}}
5 C++调用QML端函数

//main.c  load后获取qml对象
    auto obj_list = engine.rootObjects();
    auto window = obj_list.first();

 //调用QML函数
    QMetaObject::invokeMethod(window,"qmlFunction",
                              Q_RETURN_ARG(QVariant,res),
                              Q_ARG(QVariant,arg1),
                              Q_ARG(QVariant,arg2)
                              );

mian.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {id: windowwidth: 640height: 480visible: truetitle: qsTr("Hello World")function qmlFunction(arg1,arg2) {return arg1 + arg2;}}

mian.c

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtQml/qqml.h>
#include "myobject.h"int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endifQGuiApplication app(argc, argv);QQmlApplicationEngine engine;QQmlContext *context1 = engine.rootContext();context1->setContextProperty("test",200);qmlRegisterType<MyObject>("testObj",1,0,"MyObject");//qmlRegisterSingletonInstance("testObj",1,0,"MyObject",MyObject::getInstance());const QUrl url(QStringLiteral("qrc:/main.qml"));QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,&app, [url](QObject *obj, const QUrl &objUrl) {if (!obj && url == objUrl)QCoreApplication::exit(-1);}, Qt::QueuedConnection);engine.load(url);//获取qml对象auto obj_list = engine.rootObjects();auto window = obj_list.first();QVariant res;QVariant arg1 = "arg1";QVariant arg2 = "arg2";//调用QML函数QMetaObject::invokeMethod(window,"qmlFunction",Q_RETURN_ARG(QVariant,res),Q_ARG(QVariant,arg1),Q_ARG(QVariant,arg2));qDebug()<<res;return app.exec();
}

http://www.hkea.cn/news/997266/

相关文章:

  • 建设局查询网站网络上市场推广
  • 怎么做装修网站b2b多平台一键发布
  • ASP做网站源代码大专网络营销专业好不好
  • 网络公司网站 优帮云做网站排名服务热线
  • 制作网页设计软件列表案例谷歌seo 优化
  • wordpress网站备案上海搜索推广
  • 网站建设套餐有哪些安卓在线视频嗅探app
  • 做电影网站要买什么重庆seo网站哪家好
  • 广州北京网站建设公司网站外部优化的4大重点
  • 网站建设书优化大师是干什么的
  • 优秀的网站建设公司百度指数人群画像
  • wordpress企业中文模板太原seo哪家好
  • 广东网广东网站建设网站推广方案模板
  • 网站运营知识快手seo
  • 咖啡公司网站建设策划书微信营销方式
  • 柳江区城乡住房建设局网站上海seo优化服务公司
  • 西城企业网站建设企业网站怎么优化
  • 初学者做动态网站项目例子游戏特效培训机构排名
  • 汽车类网站搭建直链平台
  • 做网站遇到的困难总结网络营销软件代理
  • 做网站登录论坛外链代发
  • 东营专业网站建设公司排行青岛谷歌优化公司
  • 公众号和网站先做哪个口碑营销的形式
  • 长沙企业建网站费用关键词搜索推广排行榜
  • 怎么做网站端口代理沧州网络推广外包公司
  • php wordpress 目录seo课程培训机构
  • 常州网站建设方案优化引流app推广软件
  • 网络营销网站建设实训网络营销步骤
  • 网站都有后台吗百度竞价开户公司
  • 秭归网站建设网站seo优化心得