#-------------------------------------------------
#
# Project created by QtCreator 2017-07-16T19:11:59
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = samp12_1
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES +=
main.cpp
widget.cpp
qmybattery.cpp
HEADERS +=
widget.h
qmybattery.h
FORMS +=
widget.ui
main.cpp
#include "widget.h" #includewidget.uiint main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
widget.hWidget 0 0 250 156 10 自定义Widget组件 Qt::Horizontal 16777215 40 电池电量: QmyBattery QWidget qmybattery.h 1
#ifndef WIDGET_H #define WIDGET_H #includewidget.cppnamespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void on_horizontalSlider_valueChanged(int value); private: Ui::Widget *ui; }; #endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_horizontalSlider_valueChanged(int value)
{
ui->battery->setPowerLevel(value);
QString str=QStringLiteral("当前电量:")+QString::asprintf("%d %%",value);
ui->LabInfo->setText(str);
}
qmybattery.h
#ifndef WBATTERY_H #define WBATTERY_H #includeqmybattery.cpp#include class QmyBattery : public QWidget { Q_OBJECT //自定义属性 Q_PROPERTY(int powerLevel READ powerLevel WRITE setPowerLevel NOTIFY powerLevelChanged) private: QColor mColorBack=Qt::white;//背景颜色 QColor mColorBorder=Qt::black;//电池边框颜色 QColor mColorPower=Qt::green;//电量柱颜色 QColor mColorWarning=Qt::red;//电量短缺时的颜色 int mPowerLevel=60;//电量0-100 int mWarnLevel=20;//电量低警示阈值 protected: void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; public: explicit QmyBattery(QWidget *parent = 0); void setPowerLevel(int pow);//设置当前电量 int powerLevel(); void setWarnLevel(int warn);//设置电量低阈值 int warnLevel(); QSize sizeHint();//报告缺省大小 signals: void powerLevelChanged(int ); public slots: }; #endif // WBATTERY_H
#include "qmybattery.h" #includevoid QmyBattery::paintEvent(QPaintEvent *event) { //界面组件的绘制 Q_UNUSED(event); QPainter painter(this); QRect rect(0,0,width(),height()); //viewport矩形区 painter.setViewport(rect);//设置Viewport painter.setWindow(0,0,120,50); // 设置窗口大小,逻辑坐标 painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::TextAntialiasing); //绘制电池边框 QPen pen;//设置画笔 pen.setWidth(2); //线宽 pen.setColor(mColorBorder); //划线颜色 pen.setStyle(Qt::SolidLine);//线的类型,实线、虚线等 pen.setCapStyle(Qt::FlatCap);//线端点样式 pen.setJoinStyle(Qt::BevelJoin);//线的连接点样式 painter.setPen(pen); QBrush brush;//设置画刷 brush.setColor(mColorBack); //画刷颜色 brush.setStyle(Qt::SolidPattern); //画刷填充样式 painter.setBrush(brush); rect.setRect(1,1,109,48); painter.drawRect(rect);//绘制电池边框 brush.setColor(mColorBorder); //画刷颜色 painter.setBrush(brush); rect.setRect(110,15,10,20); painter.drawRect(rect); //画电池正极头 //画电池柱 if (mPowerLevel>mWarnLevel) { //正常颜色电量柱 brush.setColor(mColorPower); //画刷颜色 pen.setColor(mColorPower); //划线颜色 } else { //电量低电量柱 brush.setColor(mColorWarning); //画刷颜色 pen.setColor(mColorWarning); //划线颜色 } painter.setBrush(brush); painter.setPen(pen); if (mPowerLevel>0) { rect.setRect(5,5,mPowerLevel,40); painter.drawRect(rect);//画电池柱 } //绘制电量百分比文字 QFontMetrics textSize(this->font()); QString powStr=QString::asprintf("%d%%",mPowerLevel); QRect textRect=textSize.boundingRect(powStr);//得到字符串的rect painter.setFont(this->font()); pen.setColor(mColorBorder); //划线颜色 painter.setPen(pen); painter.drawText(55-textRect.width()/2, 23+textRect.height()/2, powStr); } QmyBattery::QmyBattery(QWidget *parent) : QWidget(parent) { // setPalette(QPalette(mColorBack)); // setAutoFillBackground(true); // this->resize(120,50); } void QmyBattery::setPowerLevel(int pow) { //设置当前电量值 mPowerLevel=pow; emit powerLevelChanged(pow); //触发信号 repaint(); } int QmyBattery::powerLevel() { //读取当前电量值 return mPowerLevel; } void QmyBattery::setWarnLevel(int warn) {//设置电量低阈值 mWarnLevel=warn; repaint(); } int QmyBattery::warnLevel() {//读取电量低阈值 return mWarnLevel; } QSize QmyBattery::sizeHint() {//报告缺省大小,调整比例 int H=this->height(); int W=H*12/5; QSize size(W,H); return size; }
源码:https://download.csdn.net/download/qq_30457077/85075848



