头文件
#pragma once
#include <QWidget>
#include <QTime>
class QPixmap;
class QTimer;
class MyRoundWidget : public QWidget
{
Q_OBJECT
public:
MyRoundWidget(QWidget *parent, int radius = 128);
~MyRoundWidget();
void showRount(int x, int y, const QSize &size, QString msg = QString());
protected:
virtual void paintEvent(QPaintEvent *event) override;
private slots:
void my_timer_timeout();
private:
QPixmap *_pixmap;
QTimer *_timer;
QTime _startTime;
QString _darwText;
int _imageRotate;
int _radius;
};
源文件:
#include "myroundwidget.h"
#include <QApplication>
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <QFontMetrics>
MyRoundWidget::MyRoundWidget(QWidget *parent, int radius)
: QWidget(parent), _imageRotate(0), _radius(radius)
{
_pixmap = new QPixmap(":/images/rotating.png");
_timer = new QTimer(this);
connect(_timer, SIGNAL(timeout()), this, SLOT(my_timer_timeout()));
}
MyRoundWidget::~MyRoundWidget()
{
delete _pixmap;
}
void MyRoundWidget::showRount(int x, int y, const QSize &size, QString msg)
{
this->move(x, y);
this->setFixedSize(size);
_startTime = QTime::currentTime();
_darwText = msg;
_timer->start(20);
this->show();
}
void MyRoundWidget::my_timer_timeout()
{
_imageRotate += 10;
if (_imageRotate >= 360)
_imageRotate = 0;
this->update();
qApp->processEvents();
}
void MyRoundWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.fillRect(this->rect(), QColor(98, 98, 98, 98));
int pw = _radius;
int ph = _radius;
int cx = this->width() / 2;
int cy = this->height() / 2;
int sx = (this->width() - pw) / 2;
int sy = (this->height() - ph) / 2;
if (!_darwText.isEmpty())
{
QString msg = _darwText + QString("[%1s]").arg(_startTime.secsTo(QTime::currentTime()));
QFontMetrics fm(font());
int w = fm.width(msg);
int h = fm.height();
painter.drawText(QRectF(cx - w / 2.0, cy - h / 2.0 + ph / 2.0 + 20, w, h), msg);
}
painter.translate(cx, cy);
painter.rotate(_imageRotate);
painter.translate(-cx, -cy);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.drawPixmap(sx, sy, pw, ph, *_pixmap);
return QWidget::paintEvent(event);
}
二
头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPixmap>
#include <QTimer>
#include <QTime>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
// void showRount(int x, int y, const QSize &size, QString msg = QString());
protected:
virtual void paintEvent(QPaintEvent *event) override;
protected slots:
//void my_timer_timeout();
private:
Ui::MainWindow *ui;
QPixmap *_pixmap;
QTimer *_timer;
/* QTime _startTime;
QString _darwText;
int _imageRotate;
int _radius;*/
};
#endif // MAINWINDOW_H
源文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <QFontMetrics>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ui->comboBox->setStyleSheet("QComboBox::drop-down { image:url(:/new/prefix1/xljt.png)}");
_pixmap=new QPixmap(":/new/prefix1/jz.png");
_timer=new QTimer(this);
_timer->start(10);
this->setWindowFlags(Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);
this->setAttribute(Qt::WA_TranslucentBackground);//支持alpha通道!!!
this->setAttribute(Qt::WA_NoSystemBackground);//不自动绘制背景
connect(_timer,SIGNAL(timeout()),this,SLOT(update()));
}
MainWindow::~MainWindow()
{
//delete _pixmap;
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *e)
{
static int i =0;//定义旋转度数
i++;
QPainter paint(this);
paint.translate(this->width()/2,this->height()/2);//设置原点为窗口中心
paint.rotate(i);//坐标旋转
paint.drawPixmap(-_pixmap->height()/2,-_pixmap->width()/2,*_pixmap);//经分析,应使图片中心,对应原点
if(i >= 360)
i = 0;//旋转度数检测
}
版权声明:本文为weixin_43435307原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。