Qt widget中使用QML自定义电池
1、效果


2、QML
在资源里新建Mybattery.qml:
import QtQuick 2.0
import QtQuick 2.12
Item {
id: root
property color color1: "#18FD18"//绿色,健康
property color color2: "#FFFF1A"//黄色,注意
property color color3: "#7F45AA"//紫色,恶化
property color color4: "#FF1A1A"//红色,故障
property color colorBase: color1
property bool colorChangeFlag: true
property int bat_value: 80
signal setBat(double batValue)//对外接口,batValue是已经使用了多少电量,比如已经使用了80%则=80
onSetBat: {
console.log("in bat")
if(batValue<0)//不能小于0
{
text_bat_text.text="err param";
return;
}
else if(batValue>100)
{
bat_value=0;
rect_bat_body.border.color=color4;
}
else
{
bat_value=100-batValue;
rect_bat_body.border.color="gray"
}
if(batValue<=80)
{
text_bat_text.text="已使用"+batValue+"%"
colorBase = color1;
}
else if((batValue>80)&&(batValue<=90))
{
text_bat_text.text="已使用"+batValue+"%"
colorBase = color2;
}
else if((batValue>90)&&(batValue<=100))
{
text_bat_text.text="已使用"+batValue+"%"
colorBase = color3;
}
else
{
text_bat_text.text="已使用"+batValue+"%"
colorBase = color4;
}
}
Rectangle {
color: "#ffffff"
width: parent.width
height: parent.height
Rectangle {
id: rect_bat_head
width: 30
height: 10
radius: 1
color: "gray"
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 3
} Rectangle {
id: rect_bat_body
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: rect_bat_head.bottom
anchors.topMargin: 3
width: parent.width-4
height: parent.height/2
color: "#ffffff"
radius: 10
border.width: 1
border.color: "gray"
Rectangle {
id: rect_bat_value
width: parent.width-8
height: (parent.height-8)*(bat_value/100.0)
anchors.bottom: parent.bottom
anchors.bottomMargin: 4
anchors.horizontalCenter: parent.horizontalCenter
radius: 8
color: colorBase
}
Text {
id: text_bat_text
anchors.centerIn: parent
font.pixelSize: 15
font.bold: true
color: "black"
text: bat_value+"% 健康"
}
}
Grid {
rows: 4
columns: 2
width: parent.width-4
height: parent.height/2
anchors.top: rect_bat_body.bottom
anchors.topMargin: 3
anchors.horizontalCenter: parent.horizontalCenter
horizontalItemAlignment:Grid.AlignLeft
verticalItemAlignment: Grid.AlignVCenter
spacing: 3
Rectangle {
color: color1
width: parent.width/3
height: parent.width/3
radius: width/2
Text {
anchors.centerIn: parent
font.pixelSize: 11
text: qsTr("健康")
}
} Text {
font.pixelSize: 13
text: qsTr("t<=80%")
}
Rectangle {
color: color2
width: parent.width/3
height: parent.width/3
radius: width/2
Text {
anchors.centerIn: parent
font.pixelSize: 11
text: qsTr("注意")
}
} Text {
font.pixelSize: 13
text: qsTr("90%>=t>80%")
}
Rectangle {
color: color3
width: parent.width/3
height: parent.width/3
radius: width/2
Text {
anchors.centerIn: parent
font.pixelSize: 11
text: qsTr("恶化")
}
}
Text {
font.pixelSize: 13
text: qsTr("100%>=t>90%")
}
Rectangle {
color: color4
width: parent.width/3
height: parent.width/3
radius: width/2
Text {
anchors.centerIn: parent
font.pixelSize: 11
text: qsTr("故障")
}
}
Text {
font.pixelSize: 13
text: qsTr("t>100%")
}
}
}
}
解读:C++访问QML有多种方式,如调用函数啥的,我都试过了,widget和qml结合的方式下,目前就c++信号触发qml信号能成功。
所有定义一个信号,并且在其回调里改变界面。
3、在ui中拖动一个quickWidget
在cpp中将其绑定成qml界面:
QUrl source("qrc:/qml/Mybattery.qml");
ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView );
ui->quickWidget->setSource(source);
ui->quickWidget->setClearColor(QColor(Qt::transparent));
4、在C++中通过信号连接QML的信号
没错,你没听错,就是信号连接信号!!!
mainwindow.h中定义信号
signals:
void set(double value);
mainwindow.cpp中
QObject *root = ui->quickWidget->rootObject();
QObject::connect(this,SIGNAL(set(double)),root,SIGNAL(setBat(double)));//信号连接信号
emit set(80);//效果上面1图
emit set();//效果上面2图
注意加头文件:
#include <QQuickItem>
#include <QQuickView>
Qt widget中使用QML自定义电池的更多相关文章
- qt quick中qml编程语言
Qt QML 入门 — 使用C++定义QML类型 发表于 2013 年 3 月 11 日 注册C++类 注册可实例化的类型 注册不实例化的QML类型 附带属性 注册C++类 注册可实例化的类型 如 ...
- 自定义的插件如何加载到Qt Designer中(详细)
要想在Qt Designer中使用自定义控件,必须要使Qt Designer能够知道我们的自定义控件的存在.有两种方法可以把新自定义控件的信息通知给Qt Designer:“升级(promotion) ...
- QtQuick 中的 qml 与 Qt 的 C++
QtQuick 可以使用内置的 JavaScript 引擎加载相应的 JS 代码,使用起来特别方便. 在 Qt 中使用 C++ 开发底层,QtQuick 用来加载.处理图像,然后使用 Qt 提供的接口 ...
- VS2008 Qt Designer 中自定义信号槽
一.Qt Designer自定义槽函数 发现:在VS2008 +Qt4.7 中打开ui文件,所用的英文QT Designer工具,没有转到槽函数的功能,不如QtCreator自带的QtDesigne ...
- 在自定义的widget中引入图表后,运行时出现TypeError #1009错误
本人网上查找了很多资料,其中大部分都是关于Flash中的动画效果问题,与这里的问题关系型不太大.故把问题的解决方法写在这里,与人方便,与己方便. 方法一: 1.在自定义的widget中添加如下两个方法 ...
- Qt开发中的实用笔记三--关于各种类的零碎知识点:
1,QUuid()创建唯一标识码,在创建数据库实体ID和链接数据库QSqlDatabase时非常方便 2,QScrollArea与QScrollBar,如果是要在widget中添加窗口滑动QScrol ...
- Qt Quick编程(1)——QML的核心部分ECMAScript
说道QML,不得不先说一下ECMAScript: ECMAScript语言的标准是由Netscape.Sun.微软.Borland等公司基于JavaScript和JScript锤炼.定义出来的. EC ...
- [转载]震惊!QWidget竟然可以嵌入到QML中,QMl窗口句柄竟然是这样获取
背景 记得在初学qml时,就被大佬告知Qml的实现有两种方式“view+item”和“engine+widow”,那么能不能将QWidget嵌入到QML中来呢,我收到的答案是不可以,原因是QML的 ...
- QT QQuickView嵌入到QT MDI中
在学习QT的过程中发现有一个特别炫酷的行星例子“planets”,有两种实现版本: 一种是基于Qt 3D QML(planets-qml),另一种则是基于Quick和强大的Three.js(plane ...
随机推荐
- Alpha冲刺随笔六:第六天
课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(十天冲刺) 团队名称:葫芦娃队 作业目标:在十天冲刺里对每天的任务进行总结. 随笔汇总:https://www.cnblogs ...
- CheckList 如何梳理可减少上线的验证时间(总结篇)
对CheckList的执行发起的思考? (1)功能越来越多,CheckList越补充越多,执行CheckList时间越来越长,如何减少上线的验证时间?(2)减少上线验证的时间外,如何保证质量?上线后少 ...
- SIGAI机器学习第十九集 随机森林
讲授集成学习的概念,Bootstrap抽样,Bagging算法,随机森林的原理,训练算法,包外误差,计算变量的重要性,实际应用 大纲: 集成学习简介 Boostrap抽样 Bagging算法 随机森林 ...
- SpringMVC的数据效验
Spring MVC本身没有数据校验的功能,它使用Hibernate的校验框架来完成. 1.导入pom节点 <!-- https://mvnrepository.com/artifact/org ...
- noi.ac #39 MST
MST 模板题 #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...
- AtCoder Grand Contest 008题解
传送门 \(A\) 分类讨论就行了 然而我竟然有一种讨论不动的感觉 int x,y; inline int min(R int x,R int y){return x<y?x:y;} inlin ...
- 教你用WordPress搭建个人博客
1. 购买VPS,推荐几个供应商: 国外的有:搬瓦工 VirMach vps.net vultr.com 等等 国内的有:阿里云 腾讯云 等等 2. 注册域名:阿里云 腾讯云 3. 下载安装PuTTy ...
- CodeForces 1202F(数论,整除分块)
题目 CodeForces 1213G 做法 假设有\(P\)个完整的循环块,假设此时答案为\(K\)(实际答案可能有多种),即每块完整块长度为\(K\),则\(P=\left \lfloor \fr ...
- Java 基础:抽象类与接口
1.什么是抽象 当父类的某些方法不确定时,可以用abstract关键字来修饰该方法[抽象方法],用abstract来修饰该类[抽象类]. 我们都知道,父类是将子类所共同拥有的属性和方法进行抽取,这些属 ...
- 实体类(VO,DO,DTO,PO)的划分《转载---》
转载自:https://blog.csdn.net/u010722643/article/details/61201899 经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实 ...