Qt如何实现通用按钮地图效果

这篇文章主要介绍“Qt如何实现通用按钮地图效果”,在日常操作中,相信很多人在Qt如何实现通用按钮地图效果问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Qt如何实现通用按钮地图效果”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联于2013年开始,先为路桥等服务建站,路桥等地企业,进行企业商务咨询服务。为路桥企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

一、前言

主要功能:

  1. 可设置防区样式 圆形、警察、气泡、气泡2、消息、消息2

  2. 可设置防区状态 布防、撤防、报警、旁路、故障

  3. 可设置报警切换

  4. 可设置显示的防区号

  5. 可设置是否可鼠标拖动

二、代码思路

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);
}

三、效果图

Qt如何实现通用按钮地图效果

到此,关于“Qt如何实现通用按钮地图效果”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!

当前文章:Qt如何实现通用按钮地图效果
文章地址:https://www.cdcxhl.com/article2/jidjic.html

成都网站建设公司_创新互联,为您提供网站建设面包屑导航网站维护关键词优化外贸网站建设网站排名

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设