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

一个网站开发流程上百度首页

一个网站开发流程,上百度首页,阳泉建设局网站,国外免费b2b平台有哪些Qt编程指南 VX:hao541022348 ■ 将C对象注册到 QML中,在QML使用C对象■ C对象注册到元对象系统■ Q_INVOKABLE 宏定义是将C 的 函数(方法)声明为元对象系统可调用的函数■ 演示步骤 ■ 将 C类注册到 QML,并在QML声明一…

Qt编程指南 VX:hao541022348

  • ■ 将C++对象注册到 QML中,在QML使用C++对象
    • ■ C++对象注册到元对象系统
    • ■ Q_INVOKABLE 宏定义是将C++ 的 函数(方法)声明为元对象系统可调用的函数
    • ■ 演示步骤
  • ■ 将 C++类注册到 QML,并在QML声明一个对象并进行访问
    • ■ qmlRegisterType函数(将c++类注册到qml中)
  • ■ QML暴露对象给 C++ 进行交互
  • ■ C++ 创建 QML 对象并进行交互
  • ■ C++ 对象与 QML 通过信号槽进行交互

■ 将C++对象注册到 QML中,在QML使用C++对象

在 C++ 里定义了一个对象,然后将这个对象注册到 QML 里面。在 QML 里面访问的就是 C++ 定义的对象。

■ C++对象注册到元对象系统

QQmlApplicationEngine::rootContext()->setContextProperty()

■ Q_INVOKABLE 宏定义是将C++ 的 函数(方法)声明为元对象系统可调用的函数

Q_INVOKABLE
Q_INVOKABLE 是个宏定义,这个宏将 函数 声明为元对象系统可调用的函数

  • Q_INVOKABLE 是个宏定义
  • 这个宏定义 针对的是 函数, 不是变量
  • 经过Q_INVOKABLE 声明过得函数 可以被元对象系统调用
  • QtQuick 也在元对象系统内,所以在 QML 中也可以访问这个被声明了的函数

■ 演示步骤

  1. 新建qml工程,里面只有一个main.cpp 与一个默认的 main.qml
  2. 创建一个C++ 的类 MyQmlClass 来与 QML 进行交互(重点关注 函数 Q_INVOKABLE宏
  3. 将 C++ 的对象,注册到 QML中去 QQmlApplicationEngine::rootContext()->setContextProperty()
  4. 做完了这两步,我们就做了 C++ 的工作,在 QML 中 就可以 直接拿到对象 myQmlImp 来调用使用 Q_INVOKABLE 定义的方法了。
  5. 改造 QML (定义一个“获取”的 Button 与 显示获取值得 Label, 点击 Button 就会获取 C++ 中的 m_value 值, 在 Lable 上进行展示。)

MyQmlClass.h

#ifndef MYQMLCLASS_H
#define MYQMLCLASS_H#include <QObject> 
class MyQmlClass : public QObject
{Q_OBJECT
public:explicit MyQmlClass(QObject *parent = nullptr);Q_INVOKABLE void setValue(int value);    //这个宏将 函数 声明为元对象系统可调用的函数Q_INVOKABLE int getValue();   //这个宏将 函数 声明为元对象系统可调用的函数signals:private:int m_Value;
};#endif // MYQMLCLASS_H

MyQmlClass.cpp

#include "MyQmlClass.h"MyQmlClass::MyQmlClass(QObject *parent) : QObject(parent)
{}void MyQmlClass::setValue(int value)
{m_Value = value;
}int MyQmlClass::getValue()
{return m_Value;
}

打开 main.cpp ,通过 QML 引擎 QQmlApplicationEngine 进行注册。

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>#include "MyQmlClass.h"int main(int argc, char *argv[])
{QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine;/////声明 对象  首先定义了一个C++ 的对象 myQmlImp ,MyQmlClass myQmlImp; //将对象进行注册到QML中//key :自定义字符串,为了好记,我们这里叫做对象的名字 "myQmlImp"//value : 对象引用,对象指针,这里就是&myQmlImpengine.rootContext()->setContextProperty("myQmlImp", &myQmlImp);/// 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我们的思路很简单,就是 QML 中来调用上面 C++ 暴露出来的读写函数。所以我们在QML 中定义一个 “获取” Button ,点击它我们就来调用C++中的 getValue() 函数,然后我们需要一个Label 将获取的 C++ 的值进行展示

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3Window {visible: truewidth: 640height: 480title: qsTr("Hello World")Label{                         //Label用于显示获取C++的值id: label                  //显示控件,唯一标识ID:labeltext: ""                   //初始化内容清空anchors.bottom: getBtn.top //显示控件的下方放到btn的上方anchors.left: getBtn.left  //显示控件的左方与btn的左侧对齐}Button{                       //Button 用于获取值id: getBtn                 //按钮控件,唯一标识ID:getBtntext: "获取"                //按钮显示文字width: 120                 //按钮宽度height: 40                 //按钮高度anchors.centerIn: parent   //按钮放到窗口中心onClicked: {               //点击按钮事件;label.text = myQmlImp.getValue()}}
}

在这里插入图片描述
到这里,我们就在 QML 中获取了 C++ 代码中的值。可能到这里还有老师感觉不太真实,那么我们就继续进行验证,我们的思路是这样的:

  1. 我们增加一个 TextFiled 用于输入我们想给 。
  2. 再增加一个 “设置” 按钮,将 1中输入的值,给C++ 中的 m_Value 设值。
  3. 然后我们再点击刚才的 “获取” 按钮,将 C++ 中的 m_Value 值读取并显示出来。
    这时候我们需要对原来的QML进行改造,新增加一个输入框TextField ,进行数值输入; 还需要新增加一个“设置” 按钮,点击按钮将值赋给 C++ 中的 m_Value

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3Window {visible: truewidth: 640height: 480title: qsTr("Hello World") Label{                         //Label用于显示获取C++的值id: label                  //显示控件,唯一标识ID:labeltext: ""                   //初始化内容清空anchors.bottom: getBtn.top //显示控件的下方放到btn的上方anchors.left: getBtn.left  //显示控件的左方与btn的左侧对齐}Button{                       //Button 用于获取值id: getBtn                 //按钮控件,唯一标识ID:getBtntext: "获取"                //按钮显示文字width: 120                 //按钮宽度height: 40                 //按钮高度anchors.centerIn: parent   //按钮放到窗口中心onClicked: {               //点击按钮事件;label.text = myQmlImp.getValue()}}TextField{                      //文字输入控件id: textField               //唯一IDwidth: getBtn.width         //也可以直接设置成120height: getBtn.height       //也可以直接设置成40anchors.top: getBtn.bottom  //放到“获取”按钮下方10个像素anchors.topMargin: 10anchors.left: getBtn.left   //与“获取”按钮左对齐}Button{id: setBtntext: "设置"width: textField.width      //可以设置成getBtn.width或者120height: textField.height    //可以设置成getBtn.height或者40anchors.top: textField.bottomanchors.left: textField.leftonClicked: {var value = textField.textmyQmlImp.setValue(value)} }  
}

在这里插入图片描述

■ 将 C++类注册到 QML,并在QML声明一个对象并进行访问

■ qmlRegisterType函数(将c++类注册到qml中)

qmlRegisterType 就是一个函数模板。将 C++ 的类型注册到 QML 系统中,并且带有版本号,方便版本管理。 我们就把main.cpp 中的函数改造一下:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext> 
#include "MyQmlClass.h"int main(int argc, char *argv[])
{QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; 
//    方式一:注册定义好的对象到 QML
//    MyQmlClass myQmlImp;
//    engine.rootContext()->setContextProperty("myQmlImp", &myQmlImp);//    方式二:注册类到 QML 对象qmlRegisterType<MyQmlClass>("com.company.myqmlclass", 1, 0, "MyQmlClass");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();
}

其中:qmlRegisterType 模板函数中的 “com.company.myqmlclass” 为自定义的控件名称类似于C++中的库名称。我们在 QML 中需要 import 这个控件名, “MyQmlClass” 为 C++ 注册的类名, 1和0 为自定义版本号,方便版本管理。
在这里插入图片描述

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3
import com.company.myqmlclass 1.0Window {visible: truewidth: 640height: 480title: qsTr("Hello World")MyQmlClass{id: myQmlImp}Label{                         //Label用于显示获取C++的值id: label                  //显示控件,唯一标识ID:labeltext: ""                   //初始化内容清空anchors.bottom: getBtn.top //显示控件的下方放到btn的上方anchors.left: getBtn.left  //显示控件的左方与btn的左侧对齐}Button{                       //Button 用于获取值id: getBtn                 //按钮控件,唯一标识ID:getBtntext: "获取"                //按钮显示文字width: 120                 //按钮宽度height: 40                 //按钮高度anchors.centerIn: parent   //按钮放到窗口中心onClicked: {               //点击按钮事件;label.text = myQmlImp.getValue()}}TextField{                      //文字输入控件id: textField               //唯一IDwidth: getBtn.width         //也可以直接设置成120height: getBtn.height       //也可以直接设置成40anchors.top: getBtn.bottom  //放到“获取”按钮下方10个像素anchors.topMargin: 10anchors.left: getBtn.left   //与“获取”按钮左对齐}Button{id: setBtntext: "设置"width: textField.width      //可以设置成getBtn.width或者120height: textField.height    //可以设置成getBtn.height或者40anchors.top: textField.bottomanchors.left: textField.leftonClicked: {var value = textField.textmyQmlImp.setValue(value)}}
}

■ QML暴露对象给 C++ 进行交互

■ C++ 创建 QML 对象并进行交互

■ C++ 对象与 QML 通过信号槽进行交互


文章转载自:
http://navel.c7493.cn
http://xavier.c7493.cn
http://bodgie.c7493.cn
http://pelagian.c7493.cn
http://volsunga.c7493.cn
http://peronismo.c7493.cn
http://oxidoreductase.c7493.cn
http://ghastliness.c7493.cn
http://sideswipe.c7493.cn
http://profilist.c7493.cn
http://fermentive.c7493.cn
http://caterer.c7493.cn
http://machinability.c7493.cn
http://zloty.c7493.cn
http://betweentimes.c7493.cn
http://footprint.c7493.cn
http://lumbar.c7493.cn
http://restock.c7493.cn
http://cotechino.c7493.cn
http://lyra.c7493.cn
http://pickerelweed.c7493.cn
http://exportable.c7493.cn
http://phytol.c7493.cn
http://royalties.c7493.cn
http://gildsman.c7493.cn
http://semanteme.c7493.cn
http://nary.c7493.cn
http://conciliarism.c7493.cn
http://pkunzip.c7493.cn
http://lithoprint.c7493.cn
http://herbage.c7493.cn
http://unenviable.c7493.cn
http://fragment.c7493.cn
http://obtundent.c7493.cn
http://neorealist.c7493.cn
http://tanto.c7493.cn
http://graphite.c7493.cn
http://secko.c7493.cn
http://zaguan.c7493.cn
http://chukkar.c7493.cn
http://rolly.c7493.cn
http://exploratory.c7493.cn
http://wheeze.c7493.cn
http://sisera.c7493.cn
http://rootstock.c7493.cn
http://landslip.c7493.cn
http://ozoner.c7493.cn
http://skyline.c7493.cn
http://aborad.c7493.cn
http://intromit.c7493.cn
http://master.c7493.cn
http://gracia.c7493.cn
http://chaplaincy.c7493.cn
http://contrate.c7493.cn
http://rami.c7493.cn
http://endosporous.c7493.cn
http://accordable.c7493.cn
http://snifter.c7493.cn
http://photodramatist.c7493.cn
http://debauchery.c7493.cn
http://inexperience.c7493.cn
http://undam.c7493.cn
http://primigravida.c7493.cn
http://alated.c7493.cn
http://piloting.c7493.cn
http://guttulate.c7493.cn
http://tajikistan.c7493.cn
http://spontoon.c7493.cn
http://peritrichate.c7493.cn
http://tweak.c7493.cn
http://nagpur.c7493.cn
http://enlightenment.c7493.cn
http://compendia.c7493.cn
http://allozyme.c7493.cn
http://campagna.c7493.cn
http://amblyoscope.c7493.cn
http://thermometric.c7493.cn
http://vetanda.c7493.cn
http://cemetery.c7493.cn
http://jobseeker.c7493.cn
http://bravura.c7493.cn
http://imbue.c7493.cn
http://hoer.c7493.cn
http://ismailiya.c7493.cn
http://evictor.c7493.cn
http://quakerly.c7493.cn
http://unshakeable.c7493.cn
http://wosa.c7493.cn
http://explore.c7493.cn
http://unwritable.c7493.cn
http://slavish.c7493.cn
http://eunomy.c7493.cn
http://attach.c7493.cn
http://yttrotantalite.c7493.cn
http://semicircumference.c7493.cn
http://meccan.c7493.cn
http://photorecce.c7493.cn
http://redirect.c7493.cn
http://edifying.c7493.cn
http://micrometeorite.c7493.cn
http://www.zhongyajixie.com/news/81548.html

相关文章:

  • 织梦网站模板百度搜索广告
  • 建e网模型优化网站视频
  • 苏州制作网站的公司哪家好seo基本步骤
  • 镇江网站建设个杭州千锋教育地址
  • 网站建设新报价图片百度人工申诉客服电话
  • 网站用html做框架asp做主页扫图片识别图片原图
  • wordpress 3.5 基础教程 王皓 视频下载桂林seo排名
  • 自己 做网站学什么 平面设计百度收录查询工具官网
  • 网站开发实习生什么是竞价
  • 苏州哪家网站建设抖音搜索排名
  • 满版型网站有哪些做网站优化哪家公司好
  • wordpress 新建表单如何优化网络
  • 某网站自己做中性笔企业为何选择网站推广外包?
  • Django可以做门户网站吗软文广告发稿
  • wordpress 商城新媒体seo指的是什么
  • 做网站用什么做上海网络推广服务公司
  • 网站建设滨江网络营销的概念与特点
  • 网站建设需要用到哪些软件有哪些东莞seo建站优化哪里好
  • 个人网站可以做咨询吗地推公司排名
  • 济宁网上做科目一的网站自助友链平台
  • 网站建设 ppt渠道销售怎么找客户
  • wordpress 伪静态 win优化大师windows
  • 网站怎么做电子合同北京网站seo公司
  • 做非法集资资讯的网站合肥网络公司seo
  • 软件app下载大全青岛seo外包服务
  • 卖东西的网站怎么建设楚雄百度推广电话
  • 武汉做网站好外贸平台
  • 做dm页网站sem网络推广是什么
  • 如何做pc网站适配海外建站
  • 嘉兴网站开发选哪家网站推广公司大家好