qt 创建一个矩形,矩形的边线可以拖拽,别用auto类型,用实际的类型

在Qt中,创建一个矩形并使其边线可以拖拽,而不使用auto类型,你可以明确指定所有变量的类型。以下是一个修改后的示例,其中避免了auto类型:

复制代码
#include <QApplication>  
#include <QGraphicsView>  
#include <QGraphicsScene>  
#include <QGraphicsRectItem>  
#include <QMouseEvent>  
#include <QPointF>  
#include <QSizeF>  
#include <QRectF>  
  
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) {  
            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() - (dragStartPos.x() - pos.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() - (dragStartPos.y() - pos.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) {  
        QGraphicsScene *scene = new QGraphicsScene(this);  
        this->setScene(scene);  
  
        ResizableRectItem *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();  
}

在这个示例中,我避免了使用auto类型,而是明确指定了所有变量的类型,包括QGraphicsSceneResizableRectItemMainWindow中的成员变量和局部变量。这样,代码的可读性和明确性可能会得到提高,尽管使用auto类型通常可以让代码更简洁。

相关推荐
曾经的三心草1 小时前
redis-2-数据结构内部编码-单线程-String命令
数据结构·数据库·redis
二哈喇子!2 小时前
基于SSM框架的公交车查询系统的设计与实现
java·数据库·ssm
Coder_Boy_2 小时前
基于SpringAI的在线考试系统-智能考试系统-学习分析模块
java·开发语言·数据库·spring boot·ddd·tdd
阿杰 AJie2 小时前
MySQL 聚合函数总表(完整版)
数据库·mysql
玄同7652 小时前
Python「焚诀」:吞噬所有语法糖的终极修炼手册
开发语言·数据库·人工智能·python·postgresql·自然语言处理·nlp
cdut_suye2 小时前
解锁函数的魔力:Python 中的多值传递、灵活参数与无名之美
java·数据库·c++·人工智能·python·机器学习·热榜
尽兴-2 小时前
MySQL 8.0高可用集群架构实战深度解析
数据库·mysql·架构·集群·高可用·innodb cluster
遇见火星2 小时前
MySQL常用命令大全(2026最新版)
数据库·mysql·oracle
霖霖总总2 小时前
[小技巧42]InnoDB 索引与 MVCC 的协同工作原理
运维·数据库·mysql
未来之窗软件服务2 小时前
计算机等级考试—数据库完整性进销存—东方仙盟练气期
数据库·oracle·计算机软考·仙盟创梦ide·东方仙盟