CRingWait.h
#pragma once
#ifndef RINGWAIT1_H
#define RINGWAIT1_H
#include <qdialog.h>
#include <QtMath>
#include <QDialog>
#include <QPainter>
#include <QTimerEvent>
class CRingWait : public QDialog
{
Q_OBJECT
private:
int offset;
public:
CRingWait(QWidget *parent = nullptr);
~CRingWait();
protected:
void paintEvent(QPaintEvent*);
void timerEvent(QTimerEvent*);
};
#endif
CRingWait.cpp
#include "CRingWait.h"
#include <QApplication>
CRingWait::CRingWait(QWidget *parent)
: QDialog(parent)
{
offset = 0;
//启动定时器
startTimer(50);
//设置控件大小
setFixedSize(75, 75);
//去掉标题栏 设置置顶 去掉任务栏图标
setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
//设置窗口背景透明
setAttribute(Qt::WA_TranslucentBackground, true);
}
CRingWait::~CRingWait()
{
}
void CRingWait::timerEvent(QTimerEvent*)
{
++offset;
if (offset > 11)
offset = 0;
update();
}
void CRingWait::paintEvent(QPaintEvent*)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true); // 反锯齿
int width = this->width();
int height = this->height();
//画笔移动到中间
painter.translate(width >> 1, height >> 1);
//计算偏移坐标
int offsetDest = (width - 30) / 2;
painter.setPen(Qt::NoPen);
//计算小圆坐标
for (int i = 0; i < 12; ++i)
{
QPoint point(0, 0);
painter.setBrush(QColor(73, 124, 255, 255 - i * 30));
point.setX(offsetDest*qSin((-offset + i)*M_PI / 6));
point.setY(offsetDest*qCos((-offset + i)*M_PI / 6));
painter.drawEllipse(point.x() - 5, point.y() - 5, 10, 10);
}
}
测试:
由于需要处理大量数据,而导致界面卡死,则需要用异步来做处理
CRingWait *p_wait = new CRingWait(this);
p_wait->show();
QFuture<int> fut_syllable = QtConcurrent::run([&]()->int
{
return (dealfunction;其他操作的函数,比如处理数据等) }); //异步处理
while (!fut_syllable.isFinished())
{
QApplication::processEvents(QEventLoop::AllEvents, 100);//唤醒主界面
}
bool ret = fut_syllable.result();
p_wait->close();
if (p_wait != nullptr)
delete p_wait;