赞
踩
QT软件开发: 点击鼠标在窗口里绘制矩形(窗口透明背景)-腾讯云开发者社区-腾讯云
一般在软件开发中,需要都有选择区域的需求,比如:
1. 截图软件,需要鼠标选择指定区域截图
2. 屏幕录像软件,需要鼠标选择指定区域录像
3. 图片浏览器,需要鼠标选择指定区域放大查看
4. 视频播放器,需要鼠标选择指定区域放大播放
...........
工程下载地址: https://download.csdn.net/download/xiaolong1126626497/21043499
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
-
- //隐藏标题栏
- setWindowFlags(Qt::FramelessWindowHint);//无边框 置顶
-
- //设置窗口背景透明
- setAttribute(Qt::WA_TranslucentBackground);
-
- //全屏显示
- showFullScreen();
-
- //设置样式
- this->setStyleSheet("#Widget{background-color: rgba(0, 0, 0, 150);}");
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::paintEvent(QPaintEvent *p1)
- {
- //绘制样式
- QStyleOption opt;
- opt.initFrom(this);
- QPainter p(this);
- style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);//绘制样式
-
- if(isPressedWidget)
- {
- //定义画笔
- QPen pen;
- pen.setWidth(5);
- pen.setColor(QColor("#00B0AE"));
- pen.setStyle(Qt::DashDotLine);
- p.setPen(pen);
-
- //创建画刷
- QBrush brush;
- brush.setColor(QColor("#00B0AE"));
- brush.setStyle(Qt::Dense6Pattern);
- p.setBrush(brush);
-
- QRect tempRt(m_startPT, m_endPT);
- p.drawRect(tempRt);
- }
- }
-
- void Widget::mousePressEvent(QMouseEvent *event)
- {
- m_endPT = m_startPT = event->pos();
- isPressedWidget = true; // 当前鼠标按下的即是QWidget而非界面上布局的其它控件
- }
-
-
- void Widget::mouseMoveEvent(QMouseEvent *event)
- {
- QPoint tmp_pos=event->pos();
- if(tmp_pos.x()>m_startPT.x() || tmp_pos.y()>m_startPT.y())
- {
- m_endPT = event->pos();
- }
- this->update();
- }
-
-
- void Widget::mouseReleaseEvent(QMouseEvent *event)
- {
- isPressedWidget = false; // 鼠标松开时,置为false
- QRect rect(m_startPT, m_endPT);
- qDebug()<<"选择的范围:"<<rect;
- }
-
- /*
- 工程: HTTP_Request
- 日期: 2021-08-12
- 作者: DS小龙哥
- 环境: win10 QT5.12.6 MinGW32
- 功能: 进入全屏
- */
- void Widget::on_pushButton_clicked()
- {
- showFullScreen();
- }
-
- /*
- 工程: HTTP_Request
- 日期: 2021-08-12
- 作者: DS小龙哥
- 环境: win10 QT5.12.6 MinGW32
- 功能: 退出全屏
- */
- void Widget::on_pushButton_2_clicked()
- {
- showNormal();
- }
-
- /*
- 工程: HTTP_Request
- 日期: 2021-08-12
- 作者: DS小龙哥
- 环境: win10 QT5.12.6 MinGW32
- 功能: close
- */
- void Widget::on_pushButton_close_clicked()
- {
- close();
- }

- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
- #include <QStyleOption>
- #include <QPainter>
- #include <QMouseEvent>
- #include <QDebug>
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
- protected:
- //截取鼠标事件绘制窗口位置. 因为标题栏隐藏后.窗口是无法拖动的。
- void mouseReleaseEvent(QMouseEvent *event);
- void mouseMoveEvent(QMouseEvent *event);
- void mousePressEvent(QMouseEvent *event);
- void paintEvent(QPaintEvent *p);
- private slots:
- void on_pushButton_clicked();
-
- void on_pushButton_2_clicked();
-
- void on_pushButton_close_clicked();
-
- private:
- Ui::Widget *ui;
- bool isPressedWidget;
-
- QPoint m_startPT;
- QPoint m_endPT;
- };
- #endif // WIDGET_H

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。