目录
文章最下方有源码获取方式哦~
效果:
1. 截图后可移动矩形
在 mousePressEvent
和 mouseMoveEvent
中添加对拖拽矩形的支持:
-
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);
}
总结
- 截图后可移动矩形:通过
mousePressEvent
、mouseMoveEvent
和mouseReleaseEvent
实现。 - 当前截图位置显示:通过
statusLabel
和updateStatusLabel
实现。 - 截图 Icon 设计:通过
setMyStyle
为按钮设置图标和样式。
源码已经给小伙伴们整理好了,微信搜索 嵌入式工程之家 关注公众号回复 截图 即可获得源码和详细操作指示哦~