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

发布时间:2021-12-15 10:38:24 作者:iii
来源:亿速云 阅读:182

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

一、前言

主要功能:

  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如何实现通用按钮地图效果”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. Qt模仿IOS滑动按钮效果
  2. Qt怎么实现通用视频控件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

qt

上一篇:leetcode如何实现转置矩阵

下一篇:leetcod如何实现比特位计数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》