qt 创建一个矩形,矩形的边线可以拖拽,拖拽时这个矩形随着这个边线缩放

在Qt中,要创建一个矩形,其边线可以拖拽以实现缩放功能,你需要重写QGraphicsRectItem类,并在其中处理鼠标事件来更新矩形的大小。以下是一个示例代码,展示了如何实现一个可拖拽边线以缩放的矩形:

#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
//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) {
                // Left edge
                dragging = true;
                dragStartSize = size;
                dragStartPos = pos;
                currentEdge = LeftEdge;
            } else if (qAbs(pos.x() - (rect.x() + rect.width())) < edgeSensitivity) {
                // Right edge
                dragging = true;
                dragStartSize = size;
                dragStartPos = pos;
                currentEdge = RightEdge;
            } else if (qAbs(pos.y() - rect.y()) < edgeSensitivity) {
                // Top edge
                dragging = true;
                dragStartSize = size;
                dragStartPos = pos;
                currentEdge = TopEdge;
            } else if (qAbs(pos.y() - (rect.y() + rect.height())) < edgeSensitivity) {
                // 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;
        QGraphicsRectItem::mouseReleaseEvent(event);
    }

private:
    bool dragging;
    QSizeF dragStartSize;
    QPointF dragStartPos;
    enum Edge { LeftEdge, RightEdge, TopEdge, BottomEdge };
    Edge currentEdge;
};

class MainWindow : public QGraphicsView {
public:
    MainWindow(QWidget *parent = nullptr) : QGraphicsView(parent) {
        auto *scene = new QGraphicsScene(this);
        this->setScene(scene);

        auto *rect = new ResizableRectItem(QRectF(10, 10, 200, 100));
        scene->addItem(rect);
    }
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MainWindow window;
    window.resize(400, 300);
    window.show();
    return app.exec();
}

在这个示例中,ResizableRectItem类重写了mousePressEventmouseMoveEventmouseReleaseEvent方法来处理拖拽和缩放操作。当用户按下鼠标左键并移动鼠标时,如果鼠标指针位于矩形的边缘附近,矩形会根据拖动的方向进行缩放。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值