当前位置:   article > 正文

Qt开源 悬浮窗口_qt 悬浮球

qt 悬浮球

 

  1. #ifndef THUNDERFLOATBALL_H
  2. #define THUNDERFLOATBALL_H
  3. #include <QRect>
  4. #include <QLabel>
  5. #include <QtMath>
  6. #include <QDialog>
  7. #include <QScreen>
  8. #include <QPainter>
  9. #include <QPaintEvent>
  10. #include <QTimerEvent>
  11. #include <QHBoxLayout>
  12. #include <QVBoxLayout>
  13. #include <QMouseEvent>
  14. #include <QGuiApplication>
  15. class ThunderFloatBall : public QDialog
  16. {
  17. Q_OBJECT
  18. private:
  19. QPoint last;
  20. int m_value;
  21. double m_offset;
  22. QRect geometry;
  23. private:
  24. QLabel* lab_Value;
  25. public:
  26. ThunderFloatBall(QWidget *parent = nullptr);
  27. ~ThunderFloatBall();
  28. private:
  29. void CalcWayLinesPath(int&,int&,QPainterPath&,QPainterPath&);
  30. protected:
  31. void paintEvent(QPaintEvent*);
  32. void timerEvent(QTimerEvent*);
  33. void mousePressEvent(QMouseEvent*);
  34. void mouseMoveEvent(QMouseEvent*);
  35. void mouseReleaseEvent(QMouseEvent*);
  36. };
  37. #endif

 

  1. #include "ThunderFloatBall.h"
  2. ThunderFloatBall::ThunderFloatBall(QWidget *parent)
  3. : QDialog(parent)
  4. {
  5. m_value=80;
  6. m_offset=0;
  7. //����һ����ʱ��
  8. startTimer(150);
  9. setFixedSize(70,70);
  10. //ȥ�������� �����ö� ȥ��������ͼ��
  11. setWindowFlags(Qt::FramelessWindowHint|Qt::Tool|Qt::WindowCloseButtonHint|Qt::WindowStaysOnTopHint);
  12. //��ȡ�����ߴ� ����λ��
  13. QScreen *primaryScreen=QGuiApplication::primaryScreen();
  14. if(primaryScreen==nullptr)
  15. return;
  16. geometry=primaryScreen->availableGeometry();
  17. this->move(geometry.width()-100,geometry.height()*0.2);
  18. //���ô��ڱ���͸��
  19. setAttribute(Qt::WA_TranslucentBackground, true);
  20. try {
  21. //��ʼ���ؼ�
  22. lab_Value=new QLabel();
  23. QLabel *lab_Unit=new QLabel();
  24. QLabel *lab_NetSpeed=new QLabel();
  25. QHBoxLayout* valueLayout=new QHBoxLayout();
  26. QVBoxLayout* mainLayout=new QVBoxLayout();
  27. //���ÿؼ���ʽ
  28. lab_Value->setFont(QFont("����",18));
  29. //���ÿؼ�����
  30. lab_Value->setText("10");
  31. lab_Unit->setText("MB/s");
  32. lab_NetSpeed->setText("+ 0B/s");
  33. //���ÿؼ����ֶ��뷽ʽ
  34. lab_Value->setAlignment(Qt::AlignHCenter|Qt::AlignBottom);
  35. lab_Unit->setAlignment(Qt::AlignHCenter|Qt::AlignBottom);
  36. lab_NetSpeed->setAlignment(Qt::AlignHCenter|Qt::AlignTop);
  37. lab_Value->setFixedHeight(40);
  38. lab_Unit->setFixedHeight(36);
  39. //����
  40. valueLayout->setMargin(0);
  41. valueLayout->setSpacing(2);
  42. valueLayout->addStretch(1);
  43. valueLayout->addWidget(lab_Value);
  44. valueLayout->addWidget(lab_Unit);
  45. valueLayout->addStretch(1);
  46. mainLayout->setMargin(2);
  47. mainLayout->setSpacing(0);
  48. mainLayout->addLayout(valueLayout,3);
  49. mainLayout->addWidget(lab_NetSpeed,2);
  50. setLayout(mainLayout);
  51. } catch (...) {}
  52. }
  53. ThunderFloatBall::~ThunderFloatBall(){}
  54. void ThunderFloatBall::timerEvent(QTimerEvent*)
  55. {
  56. update();
  57. }
  58. void ThunderFloatBall::mousePressEvent(QMouseEvent *e)
  59. {
  60. last = e->globalPos();
  61. }
  62. void ThunderFloatBall::mouseMoveEvent(QMouseEvent *e)
  63. {
  64. if((last.x()==0)&&(last.y()==0))
  65. return;
  66. int dx = e->globalX() - last.x();
  67. int dy = e->globalY() - last.y();
  68. last = e->globalPos();
  69. this->move(x()+dx, y()+dy);
  70. }
  71. void ThunderFloatBall::mouseReleaseEvent(QMouseEvent *e)
  72. {
  73. if((last.x()==0)&&(last.y()==0))
  74. return;
  75. int dx = e->globalX() - last.x();
  76. int dy = e->globalY() - last.y();
  77. this->move(x() + dx, y() + dy);
  78. }
  79. void ThunderFloatBall::paintEvent(QPaintEvent*)
  80. {
  81. QPainter painter(this);
  82. painter.setRenderHint(QPainter::Antialiasing,true); // ������;
  83. int height = this->height();
  84. int width = this->width();
  85. int roundWidth = qMin(width, height)-2;
  86. //������·������
  87. QPainterPath wavyPath1;
  88. QPainterPath wavyPath2;
  89. //���㲨�������ڵ�·������
  90. CalcWayLinesPath(width,height,wavyPath1,wavyPath2);
  91. //Բ·������
  92. QPainterPath roundPath;
  93. roundPath.addEllipse(1, 1, roundWidth, roundWidth);
  94. //����һ������Բ
  95. painter.setPen(QColor(230,230,230,200));
  96. painter.setBrush(QColor(255,255,255));
  97. painter.drawEllipse(1, 1, roundWidth, roundWidth);
  98. //�ò����ߺ�����Բ�Ľ������ƽ���
  99. QPainterPath intersectedPath;
  100. painter.setPen(Qt::NoPen);
  101. //��һ��������Բ�Ľ���·�����ϲ�����·��
  102. intersectedPath=roundPath.intersected(wavyPath1);
  103. painter.setBrush(QColor(185,187,255,100));
  104. painter.drawPath(intersectedPath);
  105. //�ڶ���������Բ�Ľ���·�����ϲ�����·��
  106. intersectedPath=roundPath.intersected(wavyPath2);
  107. painter.setBrush(QColor(107,150,255,150));
  108. painter.drawPath(intersectedPath);
  109. }
  110. void ThunderFloatBall::CalcWayLinesPath(int &width,int &height,QPainterPath& wavyPath1,QPainterPath& wavyPath2)
  111. {
  112. //����ƫ��ֵ
  113. m_offset+=0.5;
  114. if (m_offset > (width)) {
  115. m_offset = 0;
  116. }
  117. //���㵱ǰֵ�ڽ������и߶�
  118. int currentHeight = (1-(m_value)/100.0)*height;
  119. //����Բ���ж��ٸ��������� ����Ϊ1.5������
  120. double roundCycle = 3 * M_PI /width;
  121. //�ƶ������½���ʼ��
  122. wavyPath1.moveTo(0, height);
  123. wavyPath2.moveTo(0, height);
  124. for(int i=0;i<=width;++i) {
  125. //��һ������Y��
  126. double wavyY1 = (double)(2*qSin(roundCycle*i+m_offset)) + currentHeight;
  127. //�ڶ�������Y��
  128. double wavyY2 = -(double)(2*qSin(roundCycle*i+m_offset)) + currentHeight;
  129. if (m_value == 0) {
  130. wavyY1 = height;
  131. wavyY2 = height;
  132. }
  133. if (m_value == 100) {
  134. wavyY1 = 0;
  135. wavyY2 = 0;
  136. }
  137. //���ӵ���·����
  138. wavyPath1.lineTo(i, wavyY1);
  139. wavyPath2.lineTo(i, wavyY2);
  140. }
  141. //�ƶ������½ǽ����� �γ�һ���պ�·�� ��������ȡ����
  142. wavyPath1.lineTo(width, height);
  143. wavyPath2.lineTo(width, height);
  144. }

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

闽ICP备14008679号