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

目录
  • 现有功能
  • 运行结果
  • 源码
    • point_button.h
    • point_button.cpp
    • window.h
    • window.cp
    • main.cpp

现有功能

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矩形任意缩放的资料请关注我们其它相关文章!

(0)

相关推荐

  • Qt通过QGraphicsview实现简单缩放及还原效果

    本文主要介绍通过QGraphicsview实现简单的缩放,以及缩放后还原原始大小. 1,自定义一个drawview继承QGraphicsview,缩放主要实现的函数为 void scale(qreal sx,qreal sy); 通过scale可以对view进行放大或缩小. 则可以在drawview中定义缩放slots为 void drawview::zoomIn(qreal delta) { zoom(delta); } void drawview::zoomOut(qreal delta)

  • Qt图形图像开发曲线图表模块QChart库缩放/平移详细方法与实例

    1.使用QChartView来缩放 (1)用鼠标框选一个矩形,把图放大到这个矩形 QChartView::setRubberBand(QChartView::RectangleRubberBand);//XY方向同时放大到鼠标画出的矩形大小(也可以设置为只放大X轴或Y轴) (2)setRubberBand函数同时也能使鼠标右键,具备缩小图的功能. 2.使用Qchart来平移和缩放 QChart::scroll(-10, 5);//整体平移(-10, 5),两个参数分别为Δx和Δy QChart:

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

    目录 现有功能 运行结果 源码 point_button.h point_button.cpp window.h window.cp main.cpp 现有功能 1.在窗口上绘制任意大小的矩形. 2.通过边角的拖曳按钮改变矩形大小. 运行结果 源码 point_button.h #ifndef POINTBUTTON_H #define POINTBUTTON_H #include <QPushButton> #include <QWidget> #include <QMou

  • JavaScript实现矩形块大小任意缩放

    最近写了一个原生JavaScript实现矩形块大小任意缩放的案例,感觉里面的东西比较的绕,,里分享源码给大家,一起学习一下. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.

  • Qt编写地图实现闪烁点图的示例代码

    目录 一.前言 二.功能特点 三.体验地址 四.效果图 五.相关代码 一.前言 Qt作为一个超大型的一站式GUI超市开发集成环境,不仅集成了大量的可视化UI组件,还提供了网络库.数据库操作.文件操作等类库,封装的还是相当精彩一步到位,根据个人身边的一些程序员朋友了解,自从用了Qt以来发现越来越喜欢用Qt本身的类来处理,除非一些要求很高的应用场景比如并发网络才需要去使用第三方库,不然就是直接使用Qt封装好的类,用起来非常爽,尤其是类的名称和方法的名称,几乎很自然的就能打出来. Qt除了内置了各种U

  • QT编写地图实现设备点位的示例代码

    目录 一.前言 二.功能特点 三.体验地址 四.效果图 五.相关代码 一.前言 在学习JS语法的时候发现其实程序都大同小异,正所谓一通百通,熟悉各大概的语法以后基本都可以上手,和C++最大的不同就是他没有数据类型的概念,作为解释性的语言,是在执行的时候自动去转换数据类型,工作都交给解释器做掉了,这样就大大方便了程序员,到处var即可,哪怕是数组啊对象啊,万物皆var,只有当真正赋值的时候,才知道具体的数据类型. 在地图应用的相关项目中,在地图上标识一些设备点,并对点进行交互这个功能用的最多的,于

  • 基于Qt实现驾校科目考试系统的示例代码

    目录 1.设置登录界面 2.登录功能实现 2.1验证邮箱地址 2.2账号密码登录 2.3密码隐藏 3.考试界面开发 3.1考试用时 3.2题目布局 3.3按钮布局 3.4提交分数 3.5窗口交互 4.发布项目 4.1更改编译路径 4.2设置图标 4.3通过dos进行项目打包 1.设置登录界面 LoginDialog::LoginDialog(QWidget *parent) : QDialog(parent), ui(new Ui::LoginDialog) { ui->setupUi(this

  • Qt实现数据导出到xls的示例代码

    目录 一.前言 二.功能特点 三.体验地址 四.效果图 五.相关代码 一.前言 导入导出数据到csv由于语法简单,适用场景有限,于是乎还是必须再造一个轮子导出数据到xls,在经历过数十年的项目实战经验中不断调整和优化.尤其记得当初第一个版本v0.01大概在2011年左右完成的,当时是公司项目运行在嵌入式板子上,需要导出警情记录,拷贝到电脑上打印,由于嵌入式根本没有也不可能去安装excel等软件,硬着头皮去研究了xml格式的xls文件,按照那个规则组合成简单的导出数据,这个思路想法理论上比QtXl

  • QT实现制作一个ListView列表的示例代码

    目录 1.概述 2.代码示例 1.自定义QListWidget 2.自定义QListWidgetItem 3.使用 3.图片演示 1.概述 案例:使用Qt制作一个ListView.点击ListView的Item可以用于测试OpenCV的各种效果 自定义一个:MainListView继承QListWidget .MainListViewItem继承QListWidgetItem 2.代码示例 1.自定义QListWidget mainlistview.h class MainListView :

  • QT实现年会抽奖小软件的示例代码

    目录 一.效果展示: 二.软件代码介绍 1.工程目录 2.核心代码之主类代码部分 3.核心代码之线程类代码部分 一.效果展示: 1.操作说明 下拉选择主题,点击开始按钮,开始滚动,再次点击停止,显示幸运之星及名称.中选人员不参与接下来的抽取,除非软件重启或点击复位按钮. 二.软件代码介绍 1.工程目录 2.核心代码之主类代码部分 main.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include &l

  • Qt实现部件透明及阴影效果的示例代码

    目录 透明效果 情况一 情况二 情况三 情况四 阴影效果 透明效果 情况一 //窗口整个透明属性,取值为0-1,0为全透明 setWindowOpacity(0.5); 情况二 //部件不透明,窗体背景完全透明,以下两个函数必须配合使用 setWindowFlags(Qt::FramelessWindowHint);//窗口无边框 setAttribute(Qt::WA_TranslucentBackground);//背景透明 情况三 //单个部件设置透明 //需要添加头文件#include<

  • 基于Qt实现简易GIF播放器的示例代码

    目录 一.项目介绍 二.项目基本配置 三.UI界面设计 四.主程序实现 4.1 mainwindow.h头文件 4.2 mainwindow.cpp源文件 五.效果演示 一.项目介绍 利用Qt设计一个简易GIF播放器,可以播放GIF动画.其基本功能有载入文件.播放.暂停.停止.快进和快退. 二.项目基本配置 新建一个Qt案例,项目名称为“GIFTest”,基类选择“QMainWindow”,创建UI界面复选框的选中状态,完成项目创建. 三.UI界面设计 UI界面如下: 界面中创建了8个控件,其名

随机推荐