在很多项目应用中,需要根据数据动态生成对象显示在地图上,比如地图标注,同时还需要可拖动对象到指定位置显示,能有多种状态指示,安防领域一般用来表示防区或者设备,可以直接显示防区号,有多种状态颜色指示,例如布防、撤防、旁路、报警、离线、在线等状态,可以作为一个通用的设备按钮对象使用。
#ifndef BUTTONDEFENCE_H
#define BUTTONDEFENCE_H
/**
* 防区按钮控件 作者:feiyangqingyun(QQ:517216493) 2018-7-2
* 1:可设置防区样式 圆形、警察、气泡、气泡2、消息、消息2
* 2:可设置防区状态 布防、撤防、报警、旁路、故障
* 3:可设置报警切换
* 4:可设置显示的防区号
* 5:可设置是否可鼠标拖动
*/
#include <QWidget>
#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif
class QDESIGNER_WIDGET_EXPORT ButtonDefence : public QWidget
#else
class ButtonDefence : public QWidget
#endif
{
Q_OBJECT
Q_ENUMS(ButtonStyle)
Q_ENUMS(ButtonStatus)
Q_PROPERTY(bool canMove READ getCanMove WRITE setCanMove)
Q_PROPERTY(QString text READ getText WRITE setText)
Q_PROPERTY(ButtonStyle buttonStyle READ getButtonStyle WRITE setButtonStyle)
Q_PROPERTY(ButtonStatus buttonStatus READ getButtonStatus WRITE setButtonStatus)
public:
//防区样式 圆形、警察、气泡、气泡2、消息、消息2
enum ButtonStyle {
ButtonStyle_Circle = 0,
ButtonStyle_Police = 1,
ButtonStyle_Bubble = 2,
ButtonStyle_Bubble2 = 3,
ButtonStyle_Msg = 4,
ButtonStyle_Msg2 = 5
};
//防区状态 布防、撤防、报警、旁路、故障
enum ButtonStatus {
ButtonStatus_Arming = 0,
ButtonStatus_Disarming = 1,
ButtonStatus_Alarm = 2,
ButtonStatus_Bypass = 3,
ButtonStatus_Error = 4
};
explicit ButtonDefence(QWidget *parent = 0);
~ButtonDefence();
protected:
void paintEvent(QPaintEvent *);
bool eventFilter(QObject *watched, QEvent *event);
private:
bool canMove; //是否可移动
QString text; //显示文字
ButtonStyle buttonStyle; //防区样式
ButtonStatus buttonStatus; //防区状态
QString type; //图片末尾类型
QString imgName; //背景图片名称
bool isDark; //是否加深报警
QTimer *timer; //报警闪烁定时器
private slots:
void checkAlarm();
public:
bool getCanMove() const;
QString getText() const;
ButtonStyle getButtonStyle() const;
ButtonStatus getButtonStatus() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public slots:
//设置可移动
void setCanMove(bool canMove);
//设置显示文字
void setText(const QString &text);
//设置防区样式
void setButtonStyle(const ButtonStyle &buttonStyle);
//设置防区状态
void setButtonStatus(const ButtonStatus &buttonStatus);
};
#endif //BUTTONDEFENCE_H
#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<QMouseEvent *>(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<QMouseEvent *>(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();
}
原文:https://www.cnblogs.com/feiyangqingyun/p/10787716.html