Qt 实现高仿微信的滑动截屏工具(二:新增可拖拽截图框与位置显示)

目录

效果:

1. 截图后可移动矩形

2. 新增当前截图位置显示

3. 新增截图 Icon 设计

辅助函数

总结


文章最下方有源码获取方式哦~

效果:

1. 截图后可移动矩形

mousePressEventmouseMoveEvent 中添加对拖拽矩形的支持:

  • mousePressEvent:判断是否开始绘制新矩形或拖拽现有矩形。

    • 如果点击位置在 selectionRect 内,设置 isDragging = true 并记录偏移量 dragOffset
    • 否则,开始绘制新矩形。
  • mouseMoveEvent:更新矩形位置。

    • 如果 isDragging 为 true,计算新的矩形位置并更新 selectionRect
  • mouseReleaseEvent:结束拖拽操作,更新按钮位置。

void ScreenshotWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        if (isDrawing && selectionRect.contains(event->pos())) {
            isDragging = true;
            dragOffset = event->pos() - selectionRect.topLeft();
        } else {
            isDrawing = true;
            startPos = event->pos();
            endPos = startPos;
            isDragging = false;
            hideButtons();
        }
        update();
    }
    updateStatusLabel(event->pos());
}

void ScreenshotWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (isDrawing) {
        if (isDragging) {
            selectionRect.moveTopLeft(event->pos() - dragOffset);
            hideButtons();
        } else {
            endPos = event->pos();
            selectionRect = QRect(startPos, endPos).normalized();
        }
        update();
    }
    updateStatusLabel(event->pos());
}

void ScreenshotWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = false;
        updateButtonPositions();
        update();
    }
}

2. 新增当前截图位置显示

使用 statusLabel 显示鼠标的坐标,并确保其不遮挡选择区域:

void ScreenshotWidget::updateStatusLabel(const QPoint &pos)
{
    statusLabel->setText(QString("Position: (%1, %2)").arg(pos.x()).arg(pos.y()));

    if (isDrawing || isDragging) {
        int labelHeight = statusLabel->height();
        int labelWidth = statusLabel->width();
        QRect selectRect = isDragging ? selectionRect : QRect(startPos, pos).normalized();

        int x = qMax(selectRect.left(), 0);
        int y = qMax(selectRect.top() - labelHeight, 0);

        if (y < 0) {
            y = qMin(selectRect.bottom() + 5, height() - labelHeight);
        }

        x = qBound(0, x, width() - labelWidth);
        y = qBound(0, y, height() - labelHeight);

        statusLabel->move(x, y);
        statusLabel->raise();
    }
}

3. 新增截图 Icon 设计

为按钮设置图标和样式,并为 statusLabel 设置样式:

void ScreenshotWidget::setMyStyle()
{
    // 设置按钮图标和样式
    confirmButton->setIcon(QIcon(":/new/img/Img/yes.png"));
    cancelButton->setIcon(QIcon(":/new/img/Img/close.png"));
    saveButton->setIcon(QIcon(":/new/img/Img/save.png"));

    confirmButton->setStyleSheet("border: none; padding: 0px;");
    cancelButton->setStyleSheet("border: none; padding: 0px;");
    saveButton->setStyleSheet("border: none; padding: 0px;");
    confirmButton->setFixedSize(32, 32);
    cancelButton->setFixedSize(32, 32);
    saveButton->setFixedSize(32, 32);

    // 设置状态标签样式
    statusLabel->setStyleSheet("QLabel { background-color: white; color: black; border: 1px solid grey; padding: 2px; }");
    statusLabel->setText("Position: (0, 0)");
    statusLabel->setAlignment(Qt::AlignRight);
    statusLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    statusLabel->setFont(QFont("Arial", 10));
    statusLabel->setMaximumWidth(200);
    statusLabel->setFixedWidth(200);
    statusLabel->setFixedHeight(20);
}

总结

  • 截图后可移动矩形:通过 mousePressEventmouseMoveEvent 和 mouseReleaseEvent 实现。
  • 当前截图位置显示:通过 statusLabel 和 updateStatusLabel 实现。
  • 截图 Icon 设计:通过 setMyStyle 为按钮设置图标和样式。

源码已经给小伙伴们整理好了,微信搜索 嵌入式工程之家 关注公众号回复 截图 即可获得源码和详细操作指示哦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笨笨小乌龟11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值