前言

在很多项目应用中,需要根据数据动态生成对象显示在地图上,比如地图标注,同时还需要可拖动对象到指定位置显示,能有多种状态指示,安防领域一般用来表示防区或者设备,可以直接显示防区号,有多种状态颜色指示,例如布防、撤防、旁路、报警、离线、在线等状态,可以作为一个通用的设备按钮对象使用。

实现的功能

  • 1:可设置防区样式 圆形、警察、气泡、气泡2、消息、消息2
  • 2:可设置防区状态 布防、撤防、报警、旁路、故障
  • 3:可设置报警切换
  • 4:可设置显示的防区号
  • 5:可设置是否可鼠标拖动

效果图

头文件代码

#pragma execution_character_set("utf-8")#include "buttondefence.h"#include "qpainter.h"#include "qevent.h"#include "qtimer.h"#include "qdebug.h"ButtonDefence::ButtonDefence(QWidget *parent) : QWidget(parent){ canMove = false; text = "1"; buttonStyle = ButtonStyle_Police; buttonStatus = ButtonStatus_Arming; type = "police"; imgName = QString(":/image/btn_defence_disarming_%1.png").arg(type); isDark = false; timer = new QTimer(this); timer->setInterval(500); connect(timer, SIGNAL(timeout()), this, SLOT(checkAlarm())); this->installEventFilter(this);}ButtonDefence::~ButtonDefence(){ if (timer->isActive()) { timer->stop(); }}void ButtonDefence::paintEvent(QPaintEvent *){ double width = this->width(); double height = this->height(); double side = qMin(width, height); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); //绘制背景图 QImage img(imgName); if (!img.isNull()) { img = img.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation); //按照比例自动居中绘制 int pixX = rect().center().x() - img.width() / 2; int pixY = rect().center().y() - img.height() / 2; QPoint point(pixX, pixY); painter.drawImage(point, img); } //计算字体 QFont font; font.setPixelSize(side * 0.37); font.setBold(true); //自动计算文字绘制区域,绘制防区号 QRectF rect = this->rect(); if (buttonStyle == ButtonStyle_Police) { double y = (30 * height / 60); rect = QRectF(0, y, width, height - y); } else if (buttonStyle == ButtonStyle_Bubble) { double y = (8 * height / 60); rect = QRectF(0, 0, width, height - y); } else if (buttonStyle == ButtonStyle_Bubble2) { double y = (13 * height / 60); rect = QRectF(0, 0, width, height - y); font.setPixelSize(width * 0.33); } else if (buttonStyle == ButtonStyle_Msg) { double y = (17 * height / 60); rect = QRectF(0, 0, width, height - y); } else if (buttonStyle == ButtonStyle_Msg2) { double y = (17 * height / 60); rect = QRectF(0, 0, width, height - y); } //绘制文字标识 painter.setFont(font); painter.setPen(Qt::white); painter.drawText(rect, Qt::AlignCenter, text);}bool ButtonDefence::eventFilter(QObject *watched, QEvent *event){ if (canMove) { static QPoint lastPoint; static bool isPressed = false; if (event->type() == QEvent::MouseButtonPress) { QMouseEvent *e = static_cast(event); if (this->rect().contains(e->pos()) && (e->button() == Qt::LeftButton)) { lastPoint = e->pos(); isPressed = true; } } else if (event->type() == QEvent::MouseMove && isPressed) { QMouseEvent *e = static_cast(event); int dx = e->pos().x() - lastPoint.x(); int dy = e->pos().y() - lastPoint.y(); this->move(this->x() + dx, this->y() + dy); return true; } else if (event->type() == QEvent::MouseButtonRelease && isPressed) { isPressed = false; } } return QWidget::eventFilter(watched, event);}bool ButtonDefence::getCanMove() const{ return this->canMove;}QString ButtonDefence::getText() const{ return this->text;}ButtonDefence::ButtonStyle ButtonDefence::getButtonStyle() const{ return this->buttonStyle;}ButtonDefence::ButtonStatus ButtonDefence::getButtonStatus() const{ return this->buttonStatus;}QSize ButtonDefence::sizeHint() const{ return QSize(50, 50);}QSize ButtonDefence::minimumSizeHint() const{ return QSize(10, 10);}void ButtonDefence::checkAlarm(){ if (isDark) { imgName = QString(":/image/btn_defence_error_%1.png").arg(type); } else { imgName = QString(":/image/btn_defence_alarm_%1.png").arg(type); } isDark = !isDark; update();}void ButtonDefence::setCanMove(bool canMove){ this->canMove = canMove;}void ButtonDefence::setText(const QString &text){ if (this->text != text) { this->text = text; update(); }}void ButtonDefence::setButtonStyle(const ButtonDefence::ButtonStyle &buttonStyle){ this->buttonStyle = buttonStyle; if (buttonStyle == ButtonStyle_Circle) { type = "circle"; } else if (buttonStyle == ButtonStyle_Police) { type = "police"; } else if (buttonStyle == ButtonStyle_Bubble) { type = "bubble"; } else if (buttonStyle == ButtonStyle_Bubble2) { type = "bubble2"; } else if (buttonStyle == ButtonStyle_Msg) { type = "msg"; } else if (buttonStyle == ButtonStyle_Msg2) { type = "msg2"; } else { type = "circle"; } setButtonStatus(buttonStatus);}void ButtonDefence::setButtonStatus(const ButtonDefence::ButtonStatus &buttonStatus){ this->buttonStatus = buttonStatus; isDark = false; if (timer->isActive()) { timer->stop(); } if (buttonStatus == ButtonStatus_Arming) { imgName = QString(":/image/btn_defence_arming_%1.png").arg(type); } else if (buttonStatus == ButtonStatus_Disarming) { imgName = QString(":/image/btn_defence_disarming_%1.png").arg(type); } else if (buttonStatus == ButtonStatus_Bypass) { imgName = QString(":/image/btn_defence_bypass_%1.png").arg(type); } else if (buttonStatus == ButtonStatus_Error) { imgName = QString(":/image/btn_defence_error_%1.png").arg(type); } else if (buttonStatus == ButtonStatus_Alarm) { checkAlarm(); if (!timer->isActive()) { timer->start(); } } update();}

控件介绍

  1. 超过140个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。
  2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。
  3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等编译器,不乱码,可直接集成到Qt Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。
  4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。
  5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。
  6. 每个控件默认配色和demo对应的配色都非常精美。
  7. 超过120个可见控件,6个不可见控件。
  8. 部分控件提供多种样式风格选择,多种指示器样式选择。
  9. 所有控件自适应窗体拉伸变化。
  10. 集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。
  11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。
  12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。
  13. 所有控件最后生成一个dll动态库文件,可以直接集成到qtcreator中拖曳设计使用。

文字居中 qt_Qt编写自定义控件11-设备防区按钮控件相关推荐

  1. c# imager让图片有圆角unity_Qt编写自定义控件24-图片轮播控件

    一.前言 上一篇文章写的广告轮播控件,采用的传统widget堆积设置样式表做的,这次必须要用到更高级的QPainter来绘制了,这个才是最高效的办法,本控件参考雨田哥的轮播控件,经过大规模的改造而成, ...

  2. 轮播高度_Qt编写自定义控件24-图片轮播控件

    一.前言 上一篇文章写的广告轮播控件,采用的传统widget堆积设置样式表做的,这次必须要用到更高级的QPainter来绘制了,这个才是最高效的办法,本控件参考雨田哥的轮播控件,经过大规模的改造而成, ...

  3. qt creator 设置按键颜色_Qt编写自定义控件30-颜色多态按钮

    一.前言 这个控件一开始打算用样式表来实现,经过初步的探索,后面发现还是不够智能以及不能完全满足需求,比如要在此控件设置多个角标,这个用QSS就很难实现,后面才慢慢研究用QPainter来绘制,我记得 ...

  4. datetimepicker控件怎么改变hover颜色_Qt编写自定义控件9-导航按钮控件

    前言 导航按钮控件,主要用于各种漂亮精美的导航条,我们经常在web中看到导航条都非常精美,都是html+css+js实现的,还自带动画过度效果,Qt提供的qss其实也是无敌的,支持基本上所有的CSS2 ...

  5. Windows Phone开发(11):常用控件(下)

    原文:Windows Phone开发(11):常用控件(下) WP控件大部分都可以从Silverlight中继承过来,这里我也只能拿一部分作演示,对于其它控件如何使用,可以参考SDK相关说明以及Sil ...

  6. Qt自定义控件的实践——电池电量控件

    一.介绍 上一篇我们绘制了一个自定义的slider控件,现在我们再绘制一个电池控件,它可调节电池电量. 二.步骤 新建Battery类 battery.h #ifndef BATTERY_H #def ...

  7. Android自定义控件之轮播图控件

    背景 最近要做一个轮播图的效果,网上看了几篇文章,基本上都能找到实现,效果还挺不错,但是在写的时候感觉每次都要单独去重新在Activity里写一堆代码.于是自己封装了一下.这里只是做了下封装成一个控件 ...

  8. 自定义控件:等比例显示控件RatioLayout

    我们经常碰到服务器返回的图片比例大小是一样的,但是分辨力却是不一样的.这时候,就会遇到显示效果的问题.例如,图1和图2都是宽高比例相等,但是分辨率大小不一样的图片,应该按照比例显示,使用等比例显示控件 ...

  9. 苹果设备尺寸和控件尺寸

    设备大小 单位:像素 设备型号 竖屏 横屏 Iphone4s 640  *  960 960 * 640 Iphone4 640  *  960 960 * 640 Iphone5 640  *  1 ...

最新文章

  1. Go 语言实现字符串匹配算法 -- BF(Brute Force) 和 RK(Rabin Karp)
  2. maven打包常用的几个插件
  3. 如何使用RecyclerView构建Horizo​​ntal ListView?
  4. Linux grep命令分析以及C语言版本的实现
  5. 【收藏】如何避免everything每次都重建索引
  6. python宣传图片_宣传图片制作网站
  7. CCNA第五章WAN连接
  8. 【编程题目】给你 10 分钟时间,根据上排给出十个数,在其下排填出对应的十个数...
  9. 【Gamma】Scrum Meeting 6
  10. 线段树(updata+query)
  11. 递归实现将十进制转化为二进制
  12. linux下文件打包、压缩详解
  13. 快递单中抽取关键信息【一】----基于BiGRU+CR+预训练的词向量优化
  14. 【无标题】对Unity的Windows项目进行dll反编译修改
  15. pyodbc mysql_pyodbc and mySQL
  16. 基于LSTM的IMDB电影评论情感分析
  17. 《我们在时光的列车上,没有终点》
  18. 人脸识别之DeepID模型
  19. 2016年,续航新能量
  20. 易语言 php post,易语言POST发送邮件

热门文章

  1. 1074 Reversing Linked List (25 分)【难度: 一般 / 知识点: 链表】
  2. C++之抽象基类与纯虚函数
  3. Java集合Vector
  4. Mybatis查询日期范围
  5. matlab mbuild setup,关于mbuild的一个问题
  6. 【拥抱大厂系列】几个面试官常问的垃圾回收器,下次面试就拿这篇文章怼回去!
  7. 两个月的打卡活动圆满结束,今天公布兑奖详情
  8. 数据结构-二叉树和二叉查找树
  9. (JAVA学习笔记) static关键字详解
  10. 简单的五子棋操作用两种方法实现