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

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

一、功能需求

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

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

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

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

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

...........

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

二、运行效果

三、示例代码

3.1 widget.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代码

#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界面设计

最近更新

  1. TCP协议是安全的吗?

    2024-04-08 08:28:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-08 08:28:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-08 08:28:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-08 08:28:04       20 阅读

热门阅读

  1. HashMap底层源码面试题

    2024-04-08 08:28:04       14 阅读
  2. 升级到springdoc的Swagger3

    2024-04-08 08:28:04       13 阅读
  3. 2024.4.7力扣刷题记录-数组篇刷题记录2

    2024-04-08 08:28:04       14 阅读
  4. 蓝桥杯常用模板

    2024-04-08 08:28:04       13 阅读
  5. 设计模式:生活中的迭代器模式

    2024-04-08 08:28:04       14 阅读
  6. [iOS]进程-线程-队列-任务

    2024-04-08 08:28:04       14 阅读