Qt开源 悬浮窗口

cpp 复制代码
#ifndef THUNDERFLOATBALL_H
#define THUNDERFLOATBALL_H

#include <QRect>
#include <QLabel>
#include <QtMath>
#include <QDialog>
#include <QScreen>
#include <QPainter>
#include <QPaintEvent>
#include <QTimerEvent>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QGuiApplication>

class ThunderFloatBall : public QDialog
{
    Q_OBJECT


private:
    QPoint last;
    int m_value;
    double m_offset;
    QRect geometry;

private:
    QLabel* lab_Value;

public:
    ThunderFloatBall(QWidget *parent = nullptr);
    ~ThunderFloatBall();

private:
    void CalcWayLinesPath(int&,int&,QPainterPath&,QPainterPath&);

protected:
    void paintEvent(QPaintEvent*);
    void timerEvent(QTimerEvent*);
    void mousePressEvent(QMouseEvent*);
    void mouseMoveEvent(QMouseEvent*);
    void mouseReleaseEvent(QMouseEvent*);


};
#endif
cpp 复制代码
#include "ThunderFloatBall.h"

ThunderFloatBall::ThunderFloatBall(QWidget *parent)
    : QDialog(parent)
{
    m_value=80;
    m_offset=0;
    //����һ����ʱ��
    startTimer(150);
    setFixedSize(70,70);
    //ȥ�������� �����ö� ȥ��������ͼ��
    setWindowFlags(Qt::FramelessWindowHint|Qt::Tool|Qt::WindowCloseButtonHint|Qt::WindowStaysOnTopHint);

    //��ȡ�����ߴ� ����λ��
    QScreen *primaryScreen=QGuiApplication::primaryScreen();
    if(primaryScreen==nullptr)
        return;
    geometry=primaryScreen->availableGeometry();
    this->move(geometry.width()-100,geometry.height()*0.2);

    //���ô��ڱ���͸��
    setAttribute(Qt::WA_TranslucentBackground, true);

    try {
        //��ʼ���ؼ�
        lab_Value=new QLabel();
        QLabel *lab_Unit=new QLabel();
        QLabel *lab_NetSpeed=new QLabel();
        QHBoxLayout* valueLayout=new QHBoxLayout();
        QVBoxLayout* mainLayout=new QVBoxLayout();
        //���ÿؼ���ʽ
        lab_Value->setFont(QFont("����",18));
        //���ÿؼ�����
        lab_Value->setText("10");
        lab_Unit->setText("MB/s");
        lab_NetSpeed->setText("+ 0B/s");
        //���ÿؼ����ֶ��뷽ʽ
        lab_Value->setAlignment(Qt::AlignHCenter|Qt::AlignBottom);
        lab_Unit->setAlignment(Qt::AlignHCenter|Qt::AlignBottom);
        lab_NetSpeed->setAlignment(Qt::AlignHCenter|Qt::AlignTop);
        lab_Value->setFixedHeight(40);
        lab_Unit->setFixedHeight(36);
        //����
        valueLayout->setMargin(0);
        valueLayout->setSpacing(2);
        valueLayout->addStretch(1);
        valueLayout->addWidget(lab_Value);
        valueLayout->addWidget(lab_Unit);
        valueLayout->addStretch(1);
        mainLayout->setMargin(2);
        mainLayout->setSpacing(0);
        mainLayout->addLayout(valueLayout,3);
        mainLayout->addWidget(lab_NetSpeed,2);
        setLayout(mainLayout);
    } catch (...) {}
}

ThunderFloatBall::~ThunderFloatBall(){}

 void ThunderFloatBall::timerEvent(QTimerEvent*)
 {
    update();
 }

void ThunderFloatBall::mousePressEvent(QMouseEvent *e)
{
    last = e->globalPos();
}

void ThunderFloatBall::mouseMoveEvent(QMouseEvent *e)
{
    if((last.x()==0)&&(last.y()==0))
        return;
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();
    last = e->globalPos();
    this->move(x()+dx, y()+dy);
}

void ThunderFloatBall::mouseReleaseEvent(QMouseEvent *e)
{
    if((last.x()==0)&&(last.y()==0))
        return;
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();
    this->move(x() + dx, y() + dy);
}

void ThunderFloatBall::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);  // ������;
    int height = this->height();
    int width = this->width();
    int roundWidth = qMin(width, height)-2;

    //������·������
    QPainterPath wavyPath1;
    QPainterPath wavyPath2;
    //���㲨�������ڵ�·������
    CalcWayLinesPath(width,height,wavyPath1,wavyPath2);

    //Բ·������
    QPainterPath roundPath;
    roundPath.addEllipse(1, 1, roundWidth, roundWidth);

    //����һ������Բ
    painter.setPen(QColor(230,230,230,200));
    painter.setBrush(QColor(255,255,255));
    painter.drawEllipse(1, 1, roundWidth, roundWidth);

    //�ò����ߺ�����Բ�Ľ������ƽ���
    QPainterPath intersectedPath;
    painter.setPen(Qt::NoPen);
    //��һ��������Բ�Ľ���·�����ϲ�����·��
    intersectedPath=roundPath.intersected(wavyPath1);
    painter.setBrush(QColor(185,187,255,100));
    painter.drawPath(intersectedPath);

    //�ڶ���������Բ�Ľ���·�����ϲ�����·��
    intersectedPath=roundPath.intersected(wavyPath2);
    painter.setBrush(QColor(107,150,255,150));
    painter.drawPath(intersectedPath);
}

void ThunderFloatBall::CalcWayLinesPath(int &width,int &height,QPainterPath& wavyPath1,QPainterPath& wavyPath2)
{
    //����ƫ��ֵ
    m_offset+=0.5;
    if (m_offset > (width)) {
        m_offset = 0;
    }

    //���㵱ǰֵ�ڽ������и߶�
    int currentHeight = (1-(m_value)/100.0)*height;

    //����Բ���ж��ٸ��������� ����Ϊ1.5������
    double roundCycle = 3 * M_PI /width;

    //�ƶ������½���ʼ��
    wavyPath1.moveTo(0, height);
    wavyPath2.moveTo(0, height);
    for(int i=0;i<=width;++i) {
        //��һ������Y��
        double wavyY1 = (double)(2*qSin(roundCycle*i+m_offset)) + currentHeight;
        //�ڶ�������Y��
        double wavyY2 = -(double)(2*qSin(roundCycle*i+m_offset)) + currentHeight;
        if (m_value == 0) {
            wavyY1 = height;
            wavyY2 = height;
        }
        if (m_value == 100) {
            wavyY1 = 0;
            wavyY2 = 0;
        }
        //���ӵ���·����
        wavyPath1.lineTo(i, wavyY1);
        wavyPath2.lineTo(i, wavyY2);
    }
    //�ƶ������½ǽ����� �γ�һ���պ�·�� ��������ȡ����
    wavyPath1.lineTo(width, height);
    wavyPath2.lineTo(width, height);
}
相关推荐
重生之我是数学王子38 分钟前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt
----云烟----10 小时前
QT中QString类的各种使用
开发语言·qt
「QT(C++)开发工程师」15 小时前
【qt版本概述】
开发语言·qt
一路冰雨19 小时前
Qt打开文件对话框选择文件之后弹出两次
开发语言·qt
老赵的博客20 小时前
QT 自定义界面布局要诀
开发语言·qt
码码哈哈0.020 小时前
VSCode 2022 离线安装插件QT VSTOOl报错此扩展不能安装在任何当前安装的产品上。
ide·vscode·qt
feiyangqingyun1 天前
Qt/C++离线地图的加载和交互/可以离线使用/百度和天地图离线/支持手机上运行
c++·qt·qt天地图·qt离线地图·qt地图导航
gz94562 天前
windows下,用CMake编译qt项目,出现错误By not providing “FindQt5.cmake“...
开发语言·qt
「QT(C++)开发工程师」2 天前
Ubuntu 26.04 LTS 大升级:Qt 6 成为未来新引擎
qt
兆。2 天前
python实战案例----使用 PyQt5 构建简单的 HTTP 接口测试工具
爬虫·python·qt