日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/20 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文字居中 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();}

控件介紹

  • 超過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中拖曳設計使用。
  • 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的文字居中 qt_Qt编写自定义控件11-设备防区按钮控件的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。