1.概要
2.代码
2.1 代码第一版
在Qt中,要创建一个可以向左侧拖拽边线的矩形,你需要自定义一个QGraphicsRectItem
的子类,并重写其事件处理函数来响应鼠标的拖拽动作。以下是一个简单的实现示例:
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QMouseEvent>
class ResizableRectItem : public QGraphicsRectItem {
public:
ResizableRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)
: QGraphicsRectItem(rect, parent),
dragging(false),
dragStartWidth(rect.width()),
edgeSensitivity(5) {}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override {
if (event->button() == Qt::LeftButton) {
QRectF rect = this->rect();
QPointF pos = event->pos();
if (pos.x() <= rect.x() + edgeSensitivity) {
// Left edge
dragging = true;
dragStartPos = pos;
}
}
QGraphicsRectItem::mousePressEvent(event);
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override {
if (dragging) {
QPointF pos = event->pos();
qreal newWidth = dragStartWidth - (pos.x() - dragStartPos.x());
if (newWidth > 0) { // Prevent the rectangle from becoming negative width
this->setRect(QRectF(pos.x(), this->rect().y(), newWidth, this->rect().height()));
}
}
QGraphicsRectItem::mouseMoveEvent(event);
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {
dragging = false;
QGraphicsRectItem::mouseReleaseEvent(event);
}
private:
bool dragging;
qreal dragStartWidth;
QPointF dragStartPos;
int edgeSensitivity;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
ResizableRectItem *rect = new ResizableRectItem(QRectF(50, 50, 200, 100));
scene.addItem(rect);
view.show();
return app.exec();
}
在这个例子中,ResizableRectItem
类有一个edgeSensitivity
成员,它定义了鼠标可以离矩形左侧多远仍然被认为是拖拽左侧边线的范围。在mousePressEvent
中,我们检查鼠标点击的位置是否在矩形的左侧边线附近。如果是,我们就开始拖拽操作。在mouseMoveEvent
中,我们更新矩形的宽度,但不允许宽度变为负数。最后,在mouseReleaseEvent
中,我们结束拖拽操作。
2.2 代码修正
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
//https://blog.csdn.net/xie__jin__cheng/article/details/140438641?spm=1001.2014.3001.5501
//qt 创建一个矩形,矩形的边线可以拖拽
class ResizableRectItem : public QGraphicsRectItem {
public:
ResizableRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)
: QGraphicsRectItem(rect, parent),
dragging(false),
dragStartWidth(rect.width()),
edgeSensitivity(5) {
}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override {
if (event->button() == Qt::LeftButton) {
QRectF rect = this->rect();
QPointF pos = event->pos();
if (pos.x() <= rect.x() + edgeSensitivity) {
// Left edge
dragging = true;
dragStartPos = pos;
}
}
//QGraphicsRectItem::mousePressEvent(event);
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override {
if (dragging) {
QPointF pos = event->pos();
qreal newWidth = dragStartWidth - (pos.x() - dragStartPos.x());
if (newWidth > 0) { // Prevent the rectangle from becoming negative width
this->setRect(QRectF(pos.x(), this->rect().y(), newWidth, this->rect().height()));
}
}
//QGraphicsRectItem::mouseMoveEvent(event);
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {
dragging = false;
QGraphicsRectItem::mouseReleaseEvent(event);
}
private:
bool dragging;
qreal dragStartWidth;
QPointF dragStartPos;
int edgeSensitivity;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
view.resize(500,500);
ResizableRectItem *rect = new ResizableRectItem(QRectF(50, 50, 200, 100));
scene.addItem(rect);
view.show();
return app.exec();
}