QT软件开发: 点击鼠标在窗口里绘制矩形(窗口透明背景)

QT软件开发: 点击鼠标在窗口里绘制矩形(窗口透明背景)-腾讯云开发者社区-腾讯云

一、功能需求

一般在软件开发中,需要都有选择区域的需求,比如:

  1. 截图软件,需要鼠标选择指定区域截图

  2. 屏幕录像软件,需要鼠标选择指定区域录像

  3. 图片浏览器,需要鼠标选择指定区域放大查看

  4. 视频播放器,需要鼠标选择指定区域放大播放

...........

工程下载地址: https://download.csdn.net/download/xiaolong1126626497/21043499

二、运行效果

三、示例代码

3.1 widget.cpp

cpp 复制代码
#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();
}

3.2 widget.h代码

cpp 复制代码
#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

3.3 UI界面设计

相关推荐
知识分享小能手2 小时前
C++ 学习教程,从入门到精通,C++ 入门知识 — 完整知识点(1)
开发语言·c++·学习
qq_199886872 小时前
第8板块·第2节:统一内存的高级特性与性能调优
c++·人工智能·gpu算力·cuda
2401_839080544 小时前
C++常见八股
数据结构·c++·算法
qq_199886874 小时前
第6板块 第2节 CUDA运行时API与驱动API 核心笔记
c++·人工智能·gpu算力
syagain_zsx5 小时前
算法基础篇 · 03 枚举(C++ 题解)
c++·算法·二进制·枚举
weixin_307779135 小时前
C++代码实现MATLAB中的ode23tb函数功能
开发语言·c++·算法·matlab
S_WEAN6 小时前
C++STL - string类的模拟实现
c++
S_WEAN6 小时前
【C++】STL - string类
c++
码匠许师傅6 小时前
【C++三方组件】toml++:人性化的配置文件格式
c++