700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 文字居中 qt_Qt编写自定义控件11-设备防区按钮控件

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

时间:2023-09-02 03:17:29

相关推荐

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

前言

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

实现的功能

1:可设置防区样式 圆形、警察、气泡、气泡2、消息、消息22:可设置防区状态 布防、撤防、报警、旁路、故障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();}

控件介绍

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。