Qt开源作品26-通用按钮地图效果
2023-09-11 14:18:39 时间
一、前言
在很多项目应用中,需要根据数据动态生成对象显示在地图上,比如地图标注,同时还需要可拖动对象到指定位置显示,能有多种状态指示,安防领域一般用来表示防区或者设备,可以直接显示防区号,有多种状态颜色指示,例如布防、撤防、旁路、报警、离线、在线等状态,可以作为一个通用的设备按钮对象使用。
主要功能:
- 可设置防区样式 圆形、警察、气泡、气泡2、消息、消息2
- 可设置防区状态 布防、撤防、报警、旁路、故障
- 可设置报警切换
- 可设置显示的防区号
- 可设置是否可鼠标拖动
二、代码思路
void ButtonDefence::paintEvent(QPaintEvent *)
{
double width = this->width();
double height = this->height();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
//绘制背景图
QImage img(imgName);
if (!img.isNull()) {
img = img.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
painter.drawImage(0, 0, img);
}
//计算字体
QFont font;
font.setPixelSize(height * 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;
}
}
if (event->type() == QEvent::MouseButtonPress) {
emit clicked();
} else if (event->type() == QEvent::MouseButtonDblClick) {
emit doubleClicked();
}
return QWidget::eventFilter(watched, event);
}
三、效果图
四、开源主页
- 以上作品完整源码下载都在开源主页,会持续不断更新作品数量和质量,欢迎各位关注。
- 本开源项目已经成功升级到V2.0版本,分门别类,图文并茂,保你爽到爆。
- Qt开源武林秘籍开发经验,看完学完,20K起薪,没有找我!
相关文章
- QT开发:QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection'...
- qt中QString与char *类型相互转换
- qt中信号可以带返回参数,信号清除所有槽连接的方法
- QT中删除信号于槽的连接
- Qt调用OpenCV汇总(1)
- Qt对话框的事件循环分析(子线程中不能创建UI窗体分析)
- 【QT】Qt 中 Json 的操作
- Qt编写地图综合应用51-离线瓦片地图下载
- Qt开源作品2-视频流播放vlc内核
- Qt编写安防视频监控系统20-录像机管理
- Qt编写控件属性设计器10-导出xml
- QT in VS 多语言实现(中英文切换,每个步骤都有截图,只有UTF8才能让Qt语言家正确读取。先qApp->removeTranslator,然后installTranslator,每个类都要写上槽函数RetranslateUI)
- 翻译 | Qt研发副总裁分享2018年工作计划
- Qt之图形(转换)
- Qt Quick 简单教程 - 1 (代码备忘)
- 用Qt制作计时器/计算器
- [已解决]报错: qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it ...
- Qt安装后配置环境变量(Mac)
- QT源码之Qt信号槽机制与事件机制的联系