服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - Qt实现矩形大小任意缩放的示例代码

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

2022-12-20 16:40la_vie_est_belle C/C++

这篇文章主要介绍了Qt如何实现在窗口上绘制任意大小的矩形,并且通过边角的拖曳按钮可改变矩形大小,感兴趣的小伙伴可以跟随小编一起学习一下

现有功能

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

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

运行结果

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

源码

point_button.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#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

?
1
2
3
4
5
6
7
8
9
10
11
#include "window.h"
 
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Window w;
    w.show();
    return a.exec();
}

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

原文链接:https://blog.csdn.net/La_vie_est_belle/article/details/125117484

延伸 · 阅读

精彩推荐
  • C/C++C++实现高校教室管理系统

    C++实现高校教室管理系统

    这篇文章主要为大家详细介绍了C++实现高校教室管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    怀梦想,致远方3922022-10-24
  • C/C++C语言中的getchar和putchar的使用方法

    C语言中的getchar和putchar的使用方法

    这篇文章主要介绍了C语言中的getchar和putchar的使用方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下...

    C语言教程网11722021-06-07
  • C/C++C++操作文件进行读取、删除、修改指定行

    C++操作文件进行读取、删除、修改指定行

    今天小编就为大家分享一篇关于C++操作文件进行读取、删除、修改指定行,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一...

    蜗牛20111392021-07-15
  • C/C++解决codeblocks断点不停无效的问题

    解决codeblocks断点不停无效的问题

    今天小编就为大家分享一篇解决codeblocks断点不停无效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    ShellCollector6562021-08-08
  • C/C++深入浅析C/C++语言结构体指针的使用注意事项

    深入浅析C/C++语言结构体指针的使用注意事项

    这篇文章主要介绍了C/C++语言结构体指针的使用,大家都知道指针在32位系统占用4Byte,在64位系统占用8Byte,下面看下c语言代码例子...

    度若飞9842022-07-20
  • C/C++字符串中找出连续最长的数字字符串的实例代码

    字符串中找出连续最长的数字字符串的实例代码

    这篇文章介绍了字符串中找出连续最长的数字字符串的实例代码,有需要的朋友可以参考一下...

    C语言教程网7982020-12-29
  • C/C++C语言实现动态顺序表详解

    C语言实现动态顺序表详解

    这篇文章主要介绍了C语言实现动态顺序表的实现代码的相关资料,动态顺序表在内存中开辟一块空间,可以随我们数据数量的增多来扩容,需要的朋友可以...

    iwannabewater10192021-12-15
  • C/C++C/C++ 编译器优化介绍

    C/C++ 编译器优化介绍

    这篇文章主要涉及了C/C++ 编译器优化的简单介绍,具有一定参考价值。如有不对之处,欢迎指出。...

    Inside_Zhang11702021-06-04