Qt如何实现通用按钮地图效果
这篇文章主要介绍“Qt如何实现通用按钮地图效果”,在日常操作中,相信很多人在Qt如何实现通用按钮地图效果问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Qt如何实现通用按钮地图效果”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联于2013年开始,先为路桥等服务建站,路桥等地企业,进行企业商务咨询服务。为路桥企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
一、前言
主要功能:
可设置防区样式 圆形、警察、气泡、气泡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(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; } } if (event->type() == QEvent::MouseButtonPress) { emit clicked(); } else if (event->type() == QEvent::MouseButtonDblClick) { emit doubleClicked(); } return QWidget::eventFilter(watched, event); }
三、效果图
到此,关于“Qt如何实现通用按钮地图效果”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
网页名称:Qt如何实现通用按钮地图效果
网页路径:http://ybzwz.com/article/jidjic.html