文字居中 qt_Qt编写自定义控件11-设备防区按钮控件
生活随笔
收集整理的這篇文章主要介紹了
文字居中 qt_Qt编写自定义控件11-设备防区按钮控件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
在很多項目應用中,需要根據數據動態生成對象顯示在地圖上,比如地圖標注,同時還需要可拖動對象到指定位置顯示,能有多種狀態指示,安防領域一般用來表示防區或者設備,可以直接顯示防區號,有多種狀態顏色指示,例如布防、撤防、旁路、報警、離線、在線等狀態,可以作為一個通用的設備按鈕對象使用。
實現的功能
- 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();}控件介紹
總結
以上是生活随笔為你收集整理的文字居中 qt_Qt编写自定义控件11-设备防区按钮控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python智能办公系统_用 Pytho
- 下一篇: 360drive可以卸载吗_Window