当前位置:   article > 正文

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

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

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

一、功能需求

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

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

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

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

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

...........

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

二、运行效果

三、示例代码

3.1 widget.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. Widget::Widget(QWidget *parent)
  4. : QWidget(parent)
  5. , ui(new Ui::Widget)
  6. {
  7. ui->setupUi(this);
  8. //隐藏标题栏
  9. setWindowFlags(Qt::FramelessWindowHint);//无边框 置顶
  10. //设置窗口背景透明
  11. setAttribute(Qt::WA_TranslucentBackground);
  12. //全屏显示
  13. showFullScreen();
  14. //设置样式
  15. this->setStyleSheet("#Widget{background-color: rgba(0, 0, 0, 150);}");
  16. }
  17. Widget::~Widget()
  18. {
  19. delete ui;
  20. }
  21. void Widget::paintEvent(QPaintEvent *p1)
  22. {
  23. //绘制样式
  24. QStyleOption opt;
  25. opt.initFrom(this);
  26. QPainter p(this);
  27. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);//绘制样式
  28. if(isPressedWidget)
  29. {
  30. //定义画笔
  31. QPen pen;
  32. pen.setWidth(5);
  33. pen.setColor(QColor("#00B0AE"));
  34. pen.setStyle(Qt::DashDotLine);
  35. p.setPen(pen);
  36. //创建画刷
  37. QBrush brush;
  38. brush.setColor(QColor("#00B0AE"));
  39. brush.setStyle(Qt::Dense6Pattern);
  40. p.setBrush(brush);
  41. QRect tempRt(m_startPT, m_endPT);
  42. p.drawRect(tempRt);
  43. }
  44. }
  45. void Widget::mousePressEvent(QMouseEvent *event)
  46. {
  47. m_endPT = m_startPT = event->pos();
  48. isPressedWidget = true; // 当前鼠标按下的即是QWidget而非界面上布局的其它控件
  49. }
  50. void Widget::mouseMoveEvent(QMouseEvent *event)
  51. {
  52. QPoint tmp_pos=event->pos();
  53. if(tmp_pos.x()>m_startPT.x() || tmp_pos.y()>m_startPT.y())
  54. {
  55. m_endPT = event->pos();
  56. }
  57. this->update();
  58. }
  59. void Widget::mouseReleaseEvent(QMouseEvent *event)
  60. {
  61. isPressedWidget = false; // 鼠标松开时,置为false
  62. QRect rect(m_startPT, m_endPT);
  63. qDebug()<<"选择的范围:"<<rect;
  64. }
  65. /*
  66. 工程: HTTP_Request
  67. 日期: 2021-08-12
  68. 作者: DS小龙哥
  69. 环境: win10 QT5.12.6 MinGW32
  70. 功能: 进入全屏
  71. */
  72. void Widget::on_pushButton_clicked()
  73. {
  74. showFullScreen();
  75. }
  76. /*
  77. 工程: HTTP_Request
  78. 日期: 2021-08-12
  79. 作者: DS小龙哥
  80. 环境: win10 QT5.12.6 MinGW32
  81. 功能: 退出全屏
  82. */
  83. void Widget::on_pushButton_2_clicked()
  84. {
  85. showNormal();
  86. }
  87. /*
  88. 工程: HTTP_Request
  89. 日期: 2021-08-12
  90. 作者: DS小龙哥
  91. 环境: win10 QT5.12.6 MinGW32
  92. 功能: close
  93. */
  94. void Widget::on_pushButton_close_clicked()
  95. {
  96. close();
  97. }

3.2 widget.h代码

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QStyleOption>
  5. #include <QPainter>
  6. #include <QMouseEvent>
  7. #include <QDebug>
  8. QT_BEGIN_NAMESPACE
  9. namespace Ui { class Widget; }
  10. QT_END_NAMESPACE
  11. class Widget : public QWidget
  12. {
  13. Q_OBJECT
  14. public:
  15. Widget(QWidget *parent = nullptr);
  16. ~Widget();
  17. protected:
  18. //截取鼠标事件绘制窗口位置. 因为标题栏隐藏后.窗口是无法拖动的。
  19. void mouseReleaseEvent(QMouseEvent *event);
  20. void mouseMoveEvent(QMouseEvent *event);
  21. void mousePressEvent(QMouseEvent *event);
  22. void paintEvent(QPaintEvent *p);
  23. private slots:
  24. void on_pushButton_clicked();
  25. void on_pushButton_2_clicked();
  26. void on_pushButton_close_clicked();
  27. private:
  28. Ui::Widget *ui;
  29. bool isPressedWidget;
  30. QPoint m_startPT;
  31. QPoint m_endPT;
  32. };
  33. #endif // WIDGET_H

3.3 UI界面设计

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/384956?site
推荐阅读
相关标签
  

闽ICP备14008679号