日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文字居中 qt_Qt编写自定义控件11-设备防区按钮控件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

在很多項(xiàng)目應(yīng)用中,需要根據(jù)數(shù)據(jù)動(dòng)態(tài)生成對(duì)象顯示在地圖上,比如地圖標(biāo)注,同時(shí)還需要可拖動(dòng)對(duì)象到指定位置顯示,能有多種狀態(tài)指示,安防領(lǐng)域一般用來表示防區(qū)或者設(shè)備,可以直接顯示防區(qū)號(hào),有多種狀態(tài)顏色指示,例如布防、撤防、旁路、報(bào)警、離線、在線等狀態(tài),可以作為一個(gè)通用的設(shè)備按鈕對(duì)象使用。

實(shí)現(xiàn)的功能

  • 1:可設(shè)置防區(qū)樣式 圓形、警察、氣泡、氣泡2、消息、消息2
  • 2:可設(shè)置防區(qū)狀態(tài) 布防、撤防、報(bào)警、旁路、故障
  • 3:可設(shè)置報(bào)警切換
  • 4:可設(shè)置顯示的防區(qū)號(hào)
  • 5:可設(shè)置是否可鼠標(biāo)拖動(dòng)

效果圖

頭文件代碼

#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); //按照比例自動(dòng)居中繪制 int pixX = rect().center().x() - img.width() / 2; int pixY = rect().center().y() - img.height() / 2; QPoint point(pixX, pixY); painter.drawImage(point, img); } //計(jì)算字體 QFont font; font.setPixelSize(side * 0.37); font.setBold(true); //自動(dòng)計(jì)算文字繪制區(qū)域,繪制防區(qū)號(hào) 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); } //繪制文字標(biāo)識(shí) 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個(gè)精美控件,涵蓋了各種儀表盤、進(jìn)度條、進(jìn)度球、指南針、曲線圖、標(biāo)尺、溫度計(jì)、導(dǎo)航條、導(dǎo)航欄,flatui、高亮按鈕、滑動(dòng)選擇器、農(nóng)歷等。遠(yuǎn)超qwt集成的控件數(shù)量。
  • 每個(gè)類都可以獨(dú)立成一個(gè)單獨(dú)的控件,零耦合,每個(gè)控件一個(gè)頭文件和一個(gè)實(shí)現(xiàn)文件,不依賴其他文件,方便單個(gè)控件以源碼形式集成到項(xiàng)目中,較少代碼量。qwt的控件類環(huán)環(huán)相扣,高度耦合,想要使用其中一個(gè)控件,必須包含所有的代碼。
  • 全部純Qt編寫,QWidget+QPainter繪制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等編譯器,不亂碼,可直接集成到Qt Creator中,和自帶的控件一樣使用,大部分效果只要設(shè)置幾個(gè)屬性即可,極為方便。
  • 每個(gè)控件都有一個(gè)對(duì)應(yīng)的單獨(dú)的包含該控件源碼的DEMO,方便參考使用。同時(shí)還提供一個(gè)所有控件使用的集成的DEMO。
  • 每個(gè)控件的源代碼都有詳細(xì)中文注釋,都按照統(tǒng)一設(shè)計(jì)規(guī)范編寫,方便學(xué)習(xí)自定義控件的編寫。
  • 每個(gè)控件默認(rèn)配色和demo對(duì)應(yīng)的配色都非常精美。
  • 超過120個(gè)可見控件,6個(gè)不可見控件。
  • 部分控件提供多種樣式風(fēng)格選擇,多種指示器樣式選擇。
  • 所有控件自適應(yīng)窗體拉伸變化。
  • 集成自定義控件屬性設(shè)計(jì)器,支持拖曳設(shè)計(jì),所見即所得,支持導(dǎo)入導(dǎo)出xml格式。
  • 自帶activex控件demo,所有控件可以直接運(yùn)行在ie瀏覽器中。
  • 集成fontawesome圖形字體+阿里巴巴iconfont收藏的幾百個(gè)圖形字體,享受圖形字體帶來的樂趣。
  • 所有控件最后生成一個(gè)dll動(dòng)態(tài)庫文件,可以直接集成到qtcreator中拖曳設(shè)計(jì)使用。
  • 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

    總結(jié)

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

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。