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自定义电池的更多相关文章

  1. qt quick中qml编程语言

    Qt QML 入门 — 使用C++定义QML类型 发表于 2013 年 3 月 11 日   注册C++类 注册可实例化的类型 注册不实例化的QML类型 附带属性 注册C++类 注册可实例化的类型 如 ...

  2. 自定义的插件如何加载到Qt Designer中(详细)

    要想在Qt Designer中使用自定义控件,必须要使Qt Designer能够知道我们的自定义控件的存在.有两种方法可以把新自定义控件的信息通知给Qt Designer:“升级(promotion) ...

  3. QtQuick 中的 qml 与 Qt 的 C++

    QtQuick 可以使用内置的 JavaScript 引擎加载相应的 JS 代码,使用起来特别方便. 在 Qt 中使用 C++ 开发底层,QtQuick 用来加载.处理图像,然后使用 Qt 提供的接口 ...

  4. VS2008 Qt Designer 中自定义信号槽

    一.Qt Designer自定义槽函数 发现:在VS2008 +Qt4.7  中打开ui文件,所用的英文QT Designer工具,没有转到槽函数的功能,不如QtCreator自带的QtDesigne ...

  5. 在自定义的widget中引入图表后,运行时出现TypeError #1009错误

    本人网上查找了很多资料,其中大部分都是关于Flash中的动画效果问题,与这里的问题关系型不太大.故把问题的解决方法写在这里,与人方便,与己方便. 方法一: 1.在自定义的widget中添加如下两个方法 ...

  6. Qt开发中的实用笔记三--关于各种类的零碎知识点:

    1,QUuid()创建唯一标识码,在创建数据库实体ID和链接数据库QSqlDatabase时非常方便 2,QScrollArea与QScrollBar,如果是要在widget中添加窗口滑动QScrol ...

  7. Qt Quick编程(1)——QML的核心部分ECMAScript

    说道QML,不得不先说一下ECMAScript: ECMAScript语言的标准是由Netscape.Sun.微软.Borland等公司基于JavaScript和JScript锤炼.定义出来的. EC ...

  8. [转载]震惊!QWidget竟然可以嵌入到QML中,QMl窗口句柄竟然是这样获取

      背景 记得在初学qml时,就被大佬告知Qml的实现有两种方式“view+item”和“engine+widow”,那么能不能将QWidget嵌入到QML中来呢,我收到的答案是不可以,原因是QML的 ...

  9. QT QQuickView嵌入到QT MDI中

    在学习QT的过程中发现有一个特别炫酷的行星例子“planets”,有两种实现版本: 一种是基于Qt 3D QML(planets-qml),另一种则是基于Quick和强大的Three.js(plane ...

随机推荐

  1. vue slot 插槽详解

    插槽含义:就是引入子组件后,在插入子组件元素中添加信息或者标签,使得子组件的指定位置插入信息或者标签 插槽有三种:默认插槽.具名插槽.作用域插槽,由于vue2.6.0后对插槽进行修改,但是兼容2.6. ...

  2. java EL表达式中${param.name}详细

    在浏览器地址输入,表示传入一个参数test,值为123 URL:http://localhost:8888/Test/index.jsp?test=123 <body> ${test} $ ...

  3. Java - Annotation使用

    本文转载于(这个写的很好):https://www.cnblogs.com/be-forward-to-help-others/p/6846821.html Annotation Annotation ...

  4. Java - 基础到进阶

    # day01 一:基本操作 package _01.java基本操作; /** * 文档注释 */ public class _01Alls { public static void main(St ...

  5. ascii、unicode、utf-8、gbk 区别?

    发展史: https://www.cnblogs.com/houxt/p/11250878.html python2内容进行编码(默认ascii),而python3对内容进行编码的默认为utf-8. ...

  6. 模拟赛20181031 雅礼 Wearry 养花 折射 画作

    % Day1 Solution % Wearry % Stay determined! 养花    考虑当 kkk 确定的时候如何求答案, 显然对于所有形如 [ak,(a+1)k)[ak, (a+1) ...

  7. [GCP] Goolge compute Engine

    Which of the following is a PAAS option for hosting web apps on GCP? App Engine standard or flexible ...

  8. Kubernetes 学习15 kubernetes 认证及serviceaccount

    一.概述 1.通过此前描述可以知道k8s是以后运行我们生产环境中重要应用程序的尤其是无状态程序的一个非常重要的平台.这里面能托管一些核心应用以及核心数据,很显然对于k8s对应接口的访问不是任何人都可以 ...

  9. Kubernetes 学习9 Pod控制器

    一.Deployment 定义 1.简介 [root@k8smaster manifests]# kubectl explain deploy(也可以写作deployment) KIND: Deplo ...

  10. CF316G3 Good Substrings 广义后缀自动机

    太累了,刷刷水~ code: #include <bits/stdc++.h> #define N 500005 #define LL long long #define setIO(s) ...