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

qt 一个可以拖拽的矩形

1.概要

2.代码

2.1 mycotrl.h

#ifndef MYCOTRL_H
#define MYCOTRL_H#include <QWidget>
#include <QMouseEvent>class MyCotrl: public QWidget
{Q_OBJECT
public://MyCotrl();MyCotrl(QWidget *parent = nullptr);
protected:void paintEvent(QPaintEvent *event);void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);
private:bool dragging;QPoint oldPos;
};

2.2 widget.h 

 

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;
};
#endif // WIDGET_H

2.3 mycotrl.cpp 

 

#include "mycotrl.h"
#include <QPainter>//MyCotrl::MyCotrl() {}MyCotrl::MyCotrl(QWidget *parent) : QWidget(parent), dragging(false)
{}void MyCotrl::paintEvent(QPaintEvent *event)  {QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setBrush(Qt::blue);painter.drawRect(rect()); // 绘制一个矩形,其大小由QWidget的size决定
}void MyCotrl::mousePressEvent(QMouseEvent *event){if (event->button() == Qt::LeftButton) {dragging = true;oldPos = event->pos();}
}void MyCotrl::mouseMoveEvent(QMouseEvent *event){if (dragging) {move(mapToParent(event->pos()) - oldPos);oldPos = event->pos();}
}void MyCotrl::mouseReleaseEvent(QMouseEvent *event) {Q_UNUSED(event)dragging = false;
}

 

2.4 widget.cpp 

 

#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include "mycotrl.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));MyCotrl* my = new MyCotrl(this);button->setGeometry(QRect(100, 100, 100, 30));
}Widget::~Widget()
{delete ui;
}

2.5 main.cpp 

 

#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

3.运行结果

 

http://www.lryc.cn/news/379334.html

相关文章:

  • C# 启动exe 程序
  • Netty中的Reactor模型实现
  • dll丢失应该怎么解决,总结5种解决DLL丢失问题的方法
  • dial tcp 10.96.0.1:443: connect: no route to host
  • VScode创建ROS项目 ROS集成开发环境
  • nodejs从基础到实战学习笔记-nodejs简介
  • 2024年最新版------二进制安装部署Kubernetes(K8S)集群
  • 【mysql】关键词搜索实现
  • Python面试十问2
  • C# OpenCvSharp 图像处理函数-颜色通道-cvtColor
  • 总结之LangChain(三)——模型IO缓存
  • 判断一个Java服务是不是GateWay
  • 三次插值曲线--插值技术
  • python循环结构
  • 深入理解Netty的Pipeline机制:原理与实践详解
  • 直方图均衡化示例
  • 私域电商新纪元:消费增值模式的创新与成功实践
  • Java——IO流(一)-(6/8):字节流-FileInputStream 每次读取多个字节(示例演示)、一次读取完全部字节(方式一、方式二,注意事项)
  • 服务器SSH 免密码登录
  • Linux安装MySQL以及远程连接
  • SQL Server 数据库分页技术详解:选择最佳方法优化查询性能”。
  • electron录制-镜头缩放、移动
  • 红队内网攻防渗透:内网渗透之内网对抗:信息收集篇自动项目本机导出外部打点域内通讯PillagerBloodHound
  • 2024最新IDEA插件开发+发布全流程 SelectCamelWords[选中驼峰单词](idea源代码)
  • 【网络安全】网络安全基础精讲 - 网络安全入门第一篇
  • 初识 GPT-4 和 ChatGPT
  • 【C语言】解决C语言报错:Array Index Out of Bounds
  • 【C++】一个极简但完整的C++程序
  • Lua迭代器详解(附加红点功能实例)
  • 锂磷硫(LPS)属于硫化物固态电解质 Li7P3S11是代表性产品