Qt实现矩形大小任意缩放的示例代码

 更新时间:2022年06月07日 14:33:30   作者:la_vie_est_belle  
这篇文章主要介绍了Qt如何实现在窗口上绘制任意大小的矩形,并且通过边角的拖曳按钮可改变矩形大小,感兴趣的小伙伴可以跟随小编一起学习一下

现有功能

1.在窗口上绘制任意大小的矩形。

2.通过边角的拖曳按钮改变矩形大小。

运行结果

源码

point_button.h

#ifndef POINTBUTTON_H
#define POINTBUTTON_H
#include <QPushButton>
#include <QWidget>
#include <QMouseEvent>

class PointButton : public QPushButton
{
public:
    PointButton(QWidget *parent);
    ~PointButton();

public:
    bool isPressed;                                 // 用来判断用户是否正按在拖曳按钮上

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);

private:
    void setQss();                                  // 设置拖曳按钮样式

private:
    float startX;                                   // 用来移动拖曳按钮
    float startY;
};

#endif // POINTBUTTON_H

point_button.cpp

#include "point_button.h"
#include <QString>
#include "window.h"

PointButton::PointButton(QWidget *parent): QPushButton(parent) {
    this->resize(10, 10);
    this->isPressed = false;
    this->setQss();
}

PointButton::~PointButton() {

}

void PointButton::setQss() {
    QString qss = "QPushButton {\n"
                  "border-radius: 5px;\n"
                  "border: 1px solid black;"
                  "background-color: rgb(255, 255, 255);\n"
                  "}\n"
                  "QPushButton:hover {\n"
                  "border-width: 2px;\n"
                  "}";

    this->setStyleSheet(qss);
}

void PointButton::mousePressEvent(QMouseEvent *event) {
    QPushButton::mousePressEvent(event);
    this->startX = event->x();
    this->startY = event->y();
    this->isPressed = true;
}

void PointButton::mouseMoveEvent(QMouseEvent *event) {
    QPushButton::mouseMoveEvent(event);
    float disX = event->x() - this->startX;
    float disY = event->y() - this->startY;
    this->move(this->x()+disX, this->y()+disY);
    Window *parent = (Window*) this->parent();
    parent->changeSize();
}

void PointButton::mouseReleaseEvent(QMouseEvent *event) {
    QPushButton::mouseReleaseEvent(event);
    this->isPressed = false;
}

window.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QPen>

#include "point_button.h"

class Window : public QWidget
{
    Q_OBJECT

public:
    Window(QWidget *parent = nullptr);
    ~Window();
    void changeSize();                              // 改变矩形尺寸
    void hideCornerBtns();                          // 隐藏边角拖曳按钮
    void showCornerBtns();                          // 设置边角拖曳按钮位置并显示

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);

private:
    int x1;                                         // x1和y1是矩形左上角坐标
    int y1;
    int x2;                                         // x2和y2是矩形右下角坐标
    int y2;

    QPen pen;

    PointButton *topLeftBtn;
    PointButton *topRightBtn;
    PointButton *bottomLeftBtn;
    PointButton *bottomRightBtn;
};
#endif // WINDOW_H

window.cp

#include "window.h"
#include <Qt>
#include <QPainter>
#include <QDebug>


Window::Window(QWidget *parent): QWidget(parent) {
    this->pen = QPen(Qt::black);
    this->topLeftBtn = new PointButton(this);
    this->topRightBtn = new PointButton(this);
    this->bottomLeftBtn = new PointButton(this);
    this->bottomRightBtn = new PointButton(this);

    this->x1 = float(NULL);
    this->y1 = float(NULL);
    this->x2 = float(NULL);
    this->y2 = float(NULL);
    this->hideCornerBtns();
}

Window::~Window() {

}

void Window::mousePressEvent(QMouseEvent *event) {
    QWidget::mousePressEvent(event);
    this->x1 = float(NULL);
    this->y1 = float(NULL);
    this->x2 = float(NULL);
    this->y2 = float(NULL);
    this->hideCornerBtns();

    this->x1 = event->x();
    this->y1 = event->y();
    this->update();

}

void Window::mouseMoveEvent(QMouseEvent *event) {
    QWidget::mouseMoveEvent(event);

    if (this->topLeftBtn->isPressed || this->topRightBtn->isPressed ||
        this->bottomLeftBtn->isPressed || this->bottomRightBtn->isPressed)
        return;

    this->x2 = event->x();
    this->y2 = event->y();
    this->update();
}

void Window::paintEvent(QPaintEvent *event) {
    QWidget::paintEvent(event);

    if (this->x1==float(NULL) || this->y1==float(NULL) || this->x2==float(NULL) || this->y2==float(NULL)) {
        return;
    }

    QPainter painter(this);
    painter.setPen(this->pen);
    int width = this->x2 - this->x1;
    int height = this->y2 - this->y1;
    painter.drawRect(this->x1, this->y1, width, height);

    this->showCornerBtns();
}

void Window::hideCornerBtns() {
    this->topLeftBtn->hide();
    this->topRightBtn->hide();
    this->bottomLeftBtn->hide();
    this->bottomRightBtn->hide();
}

void Window::showCornerBtns() {
    int halfWidth = int(this->topLeftBtn->width() / 2);
    int halfHeight = int(this->topLeftBtn->height() / 2);
    this->topLeftBtn->move(this->x1-halfWidth, this->y1-halfHeight);
    this->topRightBtn->move(this->x2-halfWidth, this->y1-halfHeight);
    this->bottomLeftBtn->move(this->x1-halfWidth, this->y2-halfHeight);
    this->bottomRightBtn->move(this->x2-halfWidth, this->y2-halfHeight);

    this->topLeftBtn->show();
    this->topRightBtn->show();
    this->bottomLeftBtn->show();
    this->bottomRightBtn->show();
}

void Window::changeSize() {
    if (this->topLeftBtn->isPressed) {
        this->x1 = int(this->topLeftBtn->x() + this->topLeftBtn->width()/2);
        this->y1 = int(this->topLeftBtn->y() + this->topLeftBtn->height()/2);
    }
    else if (this->topRightBtn->isPressed) {
        this->x2 = int(this->topRightBtn->x() + this->topRightBtn->width()/2);
        this->y1 = int(this->topRightBtn->y() + this->topRightBtn->height()/2);
    }
    else if (this->bottomLeftBtn->isPressed) {
        this->x1 = int(this->bottomLeftBtn->x() + this->bottomLeftBtn->width()/2);
        this->y2 = int(this->bottomLeftBtn->y() + this->bottomLeftBtn->height()/2);
    }
    else if (this->bottomRightBtn->isPressed) {
        this->x2 = int(this->bottomRightBtn->x() + this->bottomRightBtn->width()/2);
        this->y2 = int(this->bottomRightBtn->y() + this->bottomRightBtn->height()/2);
    }
    this->update();
}

main.cpp

#include "window.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Window w;
    w.show();
    return a.exec();
}

以上就是Qt实现矩形大小任意缩放的示例代码的详细内容,更多关于Qt矩形任意缩放的资料请关注脚本之家其它相关文章!

相关文章

  • 详解C++中stoi/stol/stoll函数的用法

    详解C++中stoi/stol/stoll函数的用法

    这篇文章主要为大家详细介绍了C++中stoi、stol、stoll函数的具体用法,文中的示例代码讲解详细,对我们学校C++有一点的帮助,需要的可以参考一下
    2023-03-03
  • C++中异常的深度解析

    C++中异常的深度解析

    异常处理机制允许程序中独立开发部分能够在运行时就出现的问题进行通信并做出相应的处理,这篇文章主要介绍了C++中异常的深度解析,需要的朋友可以参考下
    2025-03-03
  • 解析C++无锁队列的实现代码

    解析C++无锁队列的实现代码

    本篇文章是对C++无锁队列的实现进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Linux C/C++实现显示NIC流量统计信息

    Linux C/C++实现显示NIC流量统计信息

    NIC流量统计信息是由操作系统维护的,当数据包通过NIC传输时,操作系统会更新相关的计数器,通过读取这些计数器,我们可以获得关于网络流量的信息,下面我们就来学习一下如何通过C/C++实现显示NIC流量统计信息吧
    2024-01-01
  • 详解C语言处理算经中著名问题百钱百鸡

    详解C语言处理算经中著名问题百钱百鸡

    古代的很多数学问题都可以用现代的编程语言去尝试解决,就如本篇,将会带你通过C语言来解决算经中百钱百鸡问题,感兴趣的朋友来看看吧
    2022-02-02
  • C++ lambda函数详解

    C++ lambda函数详解

    小编可以明确告诉大家:lambda函数是C++11中最重要的,使用最广泛的,最具现代风格的内容,lambda函数的出现改变了C++编程的思维方式。所以快和小编学习一下C++11中lambda函数的使用吧
    2023-02-02
  • C++实现连连看游戏

    C++实现连连看游戏

    这篇文章主要为大家详细介绍了C++实现连连看游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • C语言实现点餐系统

    C语言实现点餐系统

    这篇文章主要为大家详细介绍了C语言实现点餐系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • C++入门基础之命名空间、输入输出和缺省参数

    C++入门基础之命名空间、输入输出和缺省参数

    C++入门基础篇的内容为C++的基本特性,只有在掌握C++的基本特性后,是进入后面类和对象学习的基础,下面这篇文章主要给大家介绍了关于C++入门基础之命名空间、输入输出和缺省参数的相关资料,需要的朋友可以参考下
    2023-01-01
  • C++与C语言常用的语法对比

    C++与C语言常用的语法对比

    这篇文章主要介绍了C++与C语言常用的语法对比,文章基于c++和C语言的相关资料展开两者的语法相互对比,需要的小伙伴可以参考一下,希望对你的学习有所帮助
    2022-04-04

最新评论