#include <QDebug>
#include <QString>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
private slots:
void on_CloseBtn_clicked();
private:
Ui::Widget *ui;
//定义个起始点
QPoint StartPoint;
int t_id;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowFlag(Qt::FramelessWindowHint);
this->startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
//实例化一个画家类
QPainter p(this);
p.setFont(QFont("楷体"));
//设置背景颜色
p.setBrush(QBrush(QColor("pink")));
p.translate(this->width()/2,this->height()/2);
p.drawEllipse(QPoint(0,0),200,200);
p.setPen(QColor("green"));
for(int i=0; i<60; i++)
{
p.drawLine(200,0,195,0);
p.rotate(6);
}
QPen pen;
pen.setWidth(3);
p.setPen(pen);
for(int i=1; i<=12; i++)
{
p.rotate(30);
p.drawLine(200,0,190,0);
p.drawText(0,-175,QString("%1").arg(i));
}
QTime sysDate = QTime::currentTime();
QString time = sysDate.toString();
int h,m,s;
QStringList list_time =time.split(":");
h = list_time[0].toUInt();
m = list_time[1].toUInt();
s = list_time[2].toUInt();
t_id = this->startTimer(1000);
p.setPen(QColor("black"));
p.setPen(pen);
p.rotate(s*6);
p.drawLine(0,0,0,-170);
p.rotate(-s*6);
pen.setWidth(2);
p.setPen(pen);
p.rotate(m*6 + s*6/60);
p.drawLine(0,0,0,-120);
p.rotate(-m*6 - s*6/60);
pen.setWidth(3);
p.setPen(pen);
p.rotate((h%12)*30 + m*6/60 + s*6/60/12);
p.drawLine(0,0,0,-80);
p.rotate(-(h%12)*30 - m*6/60 - s*6/60/12);
update();
}
void Widget::mousePressEvent(QMouseEvent *event)
{
StartPoint = event->globalPos() - this->frameGeometry().topLeft();
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
this->move(event->globalPos() - StartPoint);
}
void Widget::on_CloseBtn_clicked()
{
close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/97a1ef40dddf524318bdd65472739aeb.png)