创建一个矩形,矩形下面有一个图片,指定矩形区域裁切图片
立即下载
资源介绍:
/*
**需求:创建一个矩形,矩形下面有一个图片,指定矩形区域裁切图片
**要求:1矩形区域可以拆分
**设计:设计一个图层,一个发图片,一个放矩形
**通过拆分矩形增加矩形数量,通过缩放矩形选择裁切区域
*/
#include
#include
#include
#include
#include
#include
#include
//https://blog.csdn.net/xie__jin__cheng/article/details/140438674?spm=1001.2014.3001.5501
//qt 创建一个矩形,矩形的边线可以拖拽,拖拽时这个矩形随着这个边线缩放
class ResizableRectItem : public QGraphicsRectItem {
public:
ResizableRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)
: QGraphicsRectItem(rect, parent),
dragging(false),
dragStartSize(QSizeF()),
dragStartPos(QPointF()) {}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override {
if (event->button() == Qt::LeftButton) {
// Check if the mouse click is close to any of the rectangle's edges
QRectF rect = this->rect();
QPointF pos = event->pos();
QSizeF size = rect.size();
const int edgeSensitivity = 5; // Pixels
if (qAbs(pos.x() - rect.x()) < edgeSensitivity) {
setCursor(Qt::SizeHorCursor);
// Left edge
dragging = true;
dragStartSize = size;
dragStartPos = pos;
currentEdge = LeftEdge;
} else if (qAbs(pos.x() - (rect.x() + rect.width())) < edgeSensitivity) {
setCursor(Qt::SizeHorCursor);
// Right edge
dragging = true;
dragStartSize = size;
dragStartPos = pos;
currentEdge = RightEdge;
} else if (qAbs(pos.y() - rect.y()) < edgeSensitivity) {
setCursor(Qt::SizeVerCursor);
// Top edge
dragging = true;
dragStartSize = size;
dragStartPos = pos;
currentEdge = TopEdge;
} else if (qAbs(pos.y() - (rect.y() + rect.height())) < edgeSensitivity) {
setCursor(Qt::SizeVerCursor);
// Bottom edge
dragging = true;
dragStartSize = size;
dragStartPos = pos;
currentEdge = BottomEdge;
}
}
//QGraphicsRectItem::mousePressEvent(event);
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override {
if (dragging) {
QPointF pos = event->pos();
QSizeF newSize = dragStartSize;
switch (currentEdge) {
case LeftEdge:
newSize.setWidth(dragStartSize.width() - ( pos.x()-dragStartPos.x()));
this->setRect(QRectF(pos.x(), this->rect().y(), newSize.width(), this->rect().height()));
break;
case RightEdge:
newSize.setWidth(dragStartSize.width() + (pos.x() - dragStartPos.x()));
this->setRect(QRectF(this->rect().x(), this->rect().y(), newSize.width(), this->rect().height()));
break;
case TopEdge:
newSize.setHeight(dragStartSize.height() - (pos.y() - dragStartPos.y()));
this->setRect(QRectF(this->rect().x(), pos.y(), this->rect().width(), newSize.height()));
break;
case BottomEdge:
newSize.setHeight(dragStartSize.height() + (pos.y() - dragStartPos.y()));
this->setRect(QRectF(this->rect().x(), this->rect().y(), this->rect().width(), newSize.height()));
break;
}
}
//QGraphicsRectItem::mouseMoveEvent(event);
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {
dragging = false;
// 鼠标不在线条上,恢复默认光标形状
setCursor(Qt::ArrowCursor);
QGraphicsRectItem::mouseReleaseEvent(event);
}
private:
bool dragging;
QSizeF dragStartSize;
QPointF dragStartPos;
enum Edge { LeftEdge, RightEdge, TopEdge, BottomEdge };
Edge currentEdge;
};
class MainWindow2: public QWidget{
public:
MainWindow2(QWidget *parent = nullptr) : QWidget(parent){
// 创建一个QLabel,并设置文本和位置
QLabel *label = new QLabel("这是一个被挡住的QLabel", this);
label->setGeometry(0, 0, 400, 300); // 设置QLabel的位置和大小
label->setStyleSheet("QLabel { background-color: red; }"); // 设置背景颜色为红色
// 创建一个QGraphicsScene和QGraphicsView
QGraphicsScene *scene = new QGraphicsScene();
QGraphicsView *view = new QGraphicsView(scene, this);
view->setStyleSheet("QGraphicsView { background-color: transparent; }");
view->setGeometry(10, 10, 400, 300);
// 设置QGraphicsView的属性以确保背景透明
//view->setAttribute(Qt::WA_TranslucentBackground);
//view->setFrameShape(QFrame::NoFrame); // 去除边框
//view->setBackgroundBrush(Qt::NoBrush); // 不设置背景刷,保持透明
// 设定原始矩形的参数
int originalX = 10;
int originalY = 10;
int originalWidth = 200;
int originalHeight = 100;
auto *rect = new ResizableRectItem(QRectF(originalX, originalY, originalWidth, originalHeight));
// 计算拆分后的两个矩形的参数
int spacing = 10;
int rect1Width = (originalWidth - spacing) / 2;
int rect2Width = originalWidth - rect1Width - spacing;
// 创建两个QGraphicsRectItem对象,分别代表拆分后的矩形
auto* rectItem1 = new ResizableRectItem(QRectF(originalX, originalY, rect1Width, originalHeight));
auto* rectItem2 = new ResizableRectItem(QRectF(originalX + rect1Width + spacing, originalY, rect2Width, originalHeight));
// 将拆分后的矩形添加到场景中
scene->addItem(rectItem1);
scene->addItem(rectItem2);
}
};
class MainWindow : public QGraphicsView {
public:
MainWindow(QWidget *parent = nullptr) : QGraphicsView(parent) {
setStyleSheet("QLabel { background-color: blue; }"); // 设置背景颜色为红色
auto *scene = new QGraphicsScene(this);
this->setScene(scene);
// 设定原始矩形的参数
int originalX = 10;
int originalY = 10;
int originalWidth = 200;
int originalHeight = 100;
auto *rect = new ResizableRectItem(QRectF(originalX, originalY, originalWidth, originalHeight));
// 计算拆分后的两个矩形的参数
int spacing = 10;
int rect1Width = (originalWidth - spacing) / 2;
int rect2Width = originalWidth - rect1Width - spacing;
// 创建两个QGraphicsRectItem对象,分别代表拆分后的矩形
auto* rectItem1 = new ResizableRectItem(QRectF(originalX, originalY, rect1Width, originalHeight));
auto* rectItem2 = new ResizableRectItem(QRectF(originalX + rect1Width + spacing, originalY, rect2Width, originalHeight));
// 将拆分后的矩形添加到场景中
scene->addItem(rectItem1);
scene->addItem(rectItem2);
//scene->addItem(rect);
}
};
/*
**需求:创建一个矩形,矩形下面有一个图片,指定矩形区域裁切图片
**要求:1矩形区域可以拆分
**设计:设计一个图层,一个发图片,一个放矩形
**通过拆分矩形增加矩形数量,通过缩放矩形选择裁切区域
*/
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow2 window;
window.resize(500, 400);
window.show();
return app.exec();
}
资源文件列表:
DragRectTest3.zip 大约有479个文件