本文主要是介绍qt 简单实验 一个可以向左侧拖拽缩放的矩形,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.概要
向左拖拽矩形,和向右拖拽不同,向右拖拽是增加宽度,向左拖拽是增加宽度的同时还要向左移动x的坐标。
2.代码
2.1 resizablerectangleleft.h
#ifndef RESIZABLERECTANGLELEFT_H
#define RESIZABLERECTANGLELEFT_H
#include <QWidget>
#include <QMouseEvent>
#include <QPainter>class ResizableRectangleLeft: public QWidget
{Q_OBJECT
public:ResizableRectangleLeft(QWidget *parent = nullptr);
protected:void paintEvent(QPaintEvent *event);void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);
private:bool resizing; // 是否正在缩放int resizeWidth; // 矩形的当前宽度int xpoint = 100;QPoint originalMousePos; // 缩放开始时鼠标的位置
};#endif // RESIZABLERECTANGLELEFT_H
2.2 resizablerectangleleft.cpp
#include "resizablerectangleleft.h"ResizableRectangleLeft::ResizableRectangleLeft(QWidget *parent) : QWidget(parent), resizing(false), resizeWidth(200) {}void ResizableRectangleLeft::paintEvent(QPaintEvent *event){QPainter painter(this);painter.setBrush(Qt::blue);QRect rectToDraw = QRect(xpoint, 0, resizeWidth, height());painter.drawRect(rectToDraw); // 绘制矩形
}void ResizableRectangleLeft::mousePressEvent(QMouseEvent *event) {//if (event->button() == Qt::LeftButton && event->pos().x() < 10) { // 假设在矩形左侧10像素范围内按下右键开始缩放if (event->button() == Qt::LeftButton) { // 假设在矩形左侧10像素范围内按下右键开始缩放resizing = true;resizeWidth = width(); // 保存原始宽度originalMousePos = event->pos();}
}void ResizableRectangleLeft::mouseMoveEvent(QMouseEvent *event) {if (resizing) {int delta = originalMousePos.x() - event->pos().x(); // 计算鼠标移动的距离if((xpoint -= delta)>0){xpoint -= delta;}if (resizeWidth + delta > 0) { // 确保矩形不会缩小到0以下resizeWidth += delta; // 调整矩形宽度update(); // 更新显示}originalMousePos = event->pos();}
}void ResizableRectangleLeft::mouseReleaseEvent(QMouseEvent *event) {if (event->button() == Qt::RightButton) {resizing = false;}
}
2.3 widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QLabel>QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();
protected:void mousePressEvent(QMouseEvent *event);
private:Ui::Widget *ui;QLabel* lb_x;QLabel* lb_y;
};
#endif // WIDGET_H
2.4 widget.cpp
#include "widget.h"
#include "ui_widget.h"
//#include "resizablerectwidget.h"
#include <QPushButton>
//#include "resizablewidget.h"
//#include "resizablerectangle.h"
#include "resizablerectangleleft.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//QPushButton *button = new QPushButton("My Button", this);// 设置按钮的位置和大小(可选)// 注意:在 QMainWindow 中,你可能需要先设置一个 central widget 或其他容器//button->setGeometry(QRect(10, 10, 100, 30));//ResizableRectWidget* widget = new ResizableRectWidget(this);//widget->setGeometry(100, 100, 200, 100); // 设置初始位置和大小//widget.show();//ResizableWidget* p_rw = new ResizableWidget(this);//p_rw->setGeometry(0, 0, 200, 100); // 设置初始位置和大小//ResizableRectangle* p_RR = new ResizableRectangle(this);//p_RR->setGeometry(200, 200, 200, 100);ResizableRectangleLeft* p_rtl = new ResizableRectangleLeft(this);p_rtl->setGeometry(0, 200, 400, 100);//lb_x = new QLabel("x",this);//lb_x->setGeometry(0, 0, 200, 100);//lb_y = new QLabel("y",this);//lb_y->setGeometry(0, 50, 200, 100);
}Widget::~Widget()
{delete ui;
}void Widget::mousePressEvent(QMouseEvent *event) {lb_x->setText(QString::number(event->pos().x()));lb_y->setText(QString::number(event->pos().y()));
}
2.5 main.cpp
3.运行
3.1 拖拽前
3.2 拖拽后
这篇关于qt 简单实验 一个可以向左侧拖拽缩放的矩形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!