当前位置: 首页 > news >正文

qt 创建一个左侧边线拖拽的矩形

1.概要

2.代码

在Qt中,如果你想要创建一个矩形,并且仅允许通过拖拽其左侧边线来改变宽度,你可以通过重写QGraphicsRectItem类来实现。以下是一个简单的例子,展示了如何创建一个自定义的ResizableRectItem类,该类允许用户通过拖拽矩形的左侧边线来改变其宽度:

#include <QApplication>  
#include <QGraphicsView>  
#include <QGraphicsScene>  
#include <QGraphicsRectItem>  
#include <QMouseEvent>  
#include <QPointF>  
#include <QSizeF>  
#include <QRectF>  
#include <QGraphicsSceneMouseEvent>class ResizableRectItem : public QGraphicsRectItem {  
public:  ResizableRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)  : QGraphicsRectItem(rect, parent),  dragging(false),  dragStartWidth(0.0),  dragStartPos(QPointF()) {}  protected:  void mousePressEvent(QGraphicsSceneMouseEvent *event) override {  if (event->button() == Qt::LeftButton) {  QRectF rect = this->rect();  QPointF pos = event->pos();  const int edgeSensitivity = 5; // Pixels  if (qAbs(pos.x() - rect.x()) < edgeSensitivity) {  // Left edge  dragging = true;  dragStartWidth = rect.width();  dragStartPos = pos;  }  }  //QGraphicsRectItem::mousePressEvent(event);  }  void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override {  if (dragging) {  QPointF pos = event->pos();  qreal newWidth = dragStartWidth - (dragStartPos.x() - pos.x());  this->setRect(QRectF(pos.x(), this->rect().y(), newWidth, this->rect().height()));  }  //QGraphicsRectItem::mouseMoveEvent(event);  }  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {  dragging = false;  QGraphicsRectItem::mouseReleaseEvent(event);  }  private:  bool dragging;  qreal dragStartWidth;  QPointF dragStartPos;  
};  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();  
}

在这个例子中,ResizableRectItem类重写了mousePressEventmouseMoveEventmouseReleaseEvent方法。当用户点击矩形的左侧边线时,mousePressEvent会设置拖拽状态,并记录拖拽开始时的宽度和位置。当用户移动鼠标时,mouseMoveEvent会根据鼠标的新位置更新矩形的宽度。最后,当用户释放鼠标按钮时,mouseReleaseEvent会结束拖拽状态。

代码2

#include <QApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>//https://blog.csdn.net/xie__jin__cheng/article/details/140438674?spm=1001.2014.3001.5501
//qt 创建一个矩形,矩形的边线可以拖拽
class DraggableRectItem : public QGraphicsRectItem {
public:DraggableRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr): QGraphicsRectItem(rect, parent),dragging(false) {}protected:void mousePressEvent(QGraphicsSceneMouseEvent *event) override {if (event->button() == Qt::LeftButton) {dragging = true;lastPos = event->pos();}//QGraphicsRectItem::mousePressEvent(event);}void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override {if (dragging) {QRectF rect = this->rect();QPointF diff = event->pos() - lastPos;rect.translate(diff);this->setRect(rect);lastPos = event->pos();}//QGraphicsRectItem::mouseMoveEvent(event);}void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {dragging = false;//QGraphicsRectItem::mouseReleaseEvent(event);}private:bool dragging;QPointF lastPos;
};class MainWindow : public QMainWindow {
public:MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {auto *scene = new QGraphicsScene(this);auto *view = new QGraphicsView(scene, this);setCentralWidget(view);auto *rect = new DraggableRectItem(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();
}

3.运行结果

相关文章:

  • 主机安全-开源HIDS字节跳动Elkeid安装使用
  • pgsql(guass)可获取到对应的表名称、字段名称、注释、字段类型
  • OrangePi AIpro 浅上手
  • MATLAB实现一个车辆悬架PID模拟系统
  • Django相关的基本操作
  • Pixi.js技术探索:开发者必备的视觉开发工具
  • MySQL手机号发送验证码设计与应用
  • Pandas数据可视化宝典:解锁图形绘制与样式自定义的奥秘
  • vscode使用记录
  • PXE、Kickstart和cobbler
  • 数据结构(C语言版)-第二章线性表
  • Windows 虚拟机服务器项目部署
  • Spring MVC 全注解开发
  • Go语言--广播式并发聊天服务器
  • TCP重传、滑动窗口、流量控制、拥塞控制机制
  • 分享一款快速APP功能测试工具
  • 「面试题」如何实现一个圣杯布局?
  • crontab执行失败的多种原因
  • CSS3 变换
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • ES学习笔记(12)--Symbol
  • idea + plantuml 画流程图
  • IOS评论框不贴底(ios12新bug)
  • java 多线程基础, 我觉得还是有必要看看的
  • jdbc就是这么简单
  • js操作时间(持续更新)
  • Lsb图片隐写
  • MYSQL 的 IF 函数
  • PHP CLI应用的调试原理
  • use Google search engine
  • Vim 折腾记
  • vue-router 实现分析
  • - 概述 - 《设计模式(极简c++版)》
  • 关于extract.autodesk.io的一些说明
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 十年未变!安全,谁之责?(下)
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 线上 python http server profile 实践
  • 小程序测试方案初探
  • 自动记录MySQL慢查询快照脚本
  • ​如何防止网络攻击?
  • ​学习笔记——动态路由——IS-IS中间系统到中间系统(报文/TLV)​
  • # C++之functional库用法整理
  • #QT(串口助手-界面)
  • #快捷键# 大学四年我常用的软件快捷键大全,教你成为电脑高手!!
  • #每天一道面试题# 什么是MySQL的回表查询
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (MTK)java文件添加简单接口并配置相应的SELinux avc 权限笔记2
  • (多级缓存)缓存同步
  • (一)认识微服务
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • .FileZilla的使用和主动模式被动模式介绍