电子时钟
隐藏widget边框
this->setWindowFlags(Qt::FramelessWindowHint); // 隐藏边框
实时跟踪鼠标
this->setMouseTracking(true); // 实时跟踪鼠标
通过信号与槽来刷新时针分针秒针状态
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
鼠标左键按下移动窗口
1 /**
2 * 鼠标当前坐标 移动窗口
3 * @brief Widget::mouseMoveEvent
4 * @param event
5 */
6 void Widget::mouseMoveEvent(QMouseEvent *event)
7 {
8 QString posStr = QString("%1,%2").arg(event->pos().x()).arg(event->pos().y());
9 QToolTip::showText(QCursor::pos(),posStr,this);
10
11 mouseDeskTolLeft = event->globalPos(); // 鼠标相对于桌面左上角的位置
12 mouseWindowTopLeft = event->pos(); // 鼠标相对于窗口左上角的位置
13 if(event->buttons()& Qt::LeftButton){
14 moveTargetPso = mouseDeskTolLeft - windowDeskTopLeft;
15 qDebug()<<moveTargetPso;
16 this->move(moveTargetPso);
17 }
18 }
设置移动窗口的位置
1 /**
2 * 窗口移动的位置都是相对于桌面左上角
3 * @brief Widget::mousePressEvent
4 * @param event
5 */
6 void Widget::mousePressEvent(QMouseEvent *event)
7 {
8 //也可以是this->geometry().topLeft();
9 windowDeskTopLeft = event->globalPos()-this->pos(); // 窗口左上角相对于桌面的位置
10 }
绘制时钟
1 /**
2 * 绘制时钟
3 * @brief Widget::paintEvent
4 * @param event
5 */
6 void Widget::paintEvent(QPaintEvent *event)
7 {
8 // 时针分针秒针
9 static const QPoint hourHand[3]={
10 QPoint(7,8),
11 QPoint(-7,8),
12 QPoint(0,-30)
13 };
14 static const QPoint minuteHand[3]={
15 QPoint(7,8),
16 QPoint(-7,8),
17 QPoint(0,-60)
18 };
19 static const QPoint secondHand[3]={
20 QPoint(7,8),
21 QPoint(-7,8),
22 QPoint(0,-90)
23 };
24 QTime time = QTime::currentTime();
25
26 paint.begin(this);
27
28 paint.setRenderHint(QPainter::Antialiasing,true);
29 paint.translate(this->width()/2,this->height()/2); // 移动坐标
30
31 // 缩放
32 int side = qMin(width(),height()); // 长宽的最小值:定义一个边界 保证是圆形的
33 paint.scale(side /200.0,side/200.0); // 跟随窗口大小发生变化
34
35 // 绘制时针刻度线
36 paint.setPen(Qt::white);
37 for (int i =0; i <12; i++){
38 paint.drawLine(QPoint(88,0),QPoint(96,0));
39 paint.rotate(30.0); // 旋转30°
40 }
41
42 // 绘制分针刻度线 (360 / 60 = 6)
43 for(int j =0; j < 60; j++){
44 if((j%5)!=0)
45 {
46 paint.drawLine(92,0,96,0);
47 }
48 paint.rotate(6.0);
49 }
50
51 // 绘制时针 旋转角度 = 小时数 * 30°
52 paint.save();
53 paint.setBrush(Qt::white); // 设置指针填充色
54 paint.rotate(30.0 * (time.hour() + time.minute() /60.0));
55 paint.drawConvexPolygon(hourHand,3);
56 paint.restore(); // 还原画笔
57
58 // 绘制分针 旋转角度 = 分钟数 * 6°
59 paint.save();
60 paint.rotate(6.0 * (time.minute() + time.second() / 60.0));
61 paint.drawConvexPolygon(minuteHand,3);
62 paint.restore(); // 还原画笔
63
64 // 绘制秒针 旋转角度 = 秒数 * 6°
65 paint.save();
66 paint.rotate(6.0 * time.second());
67 paint.drawConvexPolygon(secondHand,3);
68 paint.restore(); // 还原画笔
69
70 paint.end();
71 }
鼠标双击全屏显示
1 /**
2 * 鼠标双击全屏
3 * @brief Widget::mouseDoubleClickEvent
4 * @param event
5 */
6 void Widget::mouseDoubleClickEvent(QMouseEvent *event)
7 {
8 static bool fullScreenFlag;
9 if(!fullScreenFlag)
10 {
11 this->showFullScreen();
12 fullScreenFlag = true;
13 }
14 else
15 {
16 this->showNormal();
17 fullScreenFlag = false;
18 }
19 }
lcd显示时间
1 void Widget::refreshTime()
2 {
3 QTime time = QTime::currentTime(); //读出当前时间
4 QString text = time.toString("hh:mm:ss:zzz"); //格式化当前时间
5 ui->lcdNumber->display(text); //送显示
6 }
完整代码
widget.h
1 #ifndef WIDGET_H
2 #define WIDGET_H
3
4 #include <QWidget>
5 #include <QTimer>
6 #include <QTime>
7 #include <QDebug>
8 #include <QMenu>
9 #include <QMouseEvent>
10 #include <QPaintEvent>
11 #include <QAction>
12 #include <QToolTip>
13 #include <QPainter>
14
15 QT_BEGIN_NAMESPACE
16 namespace Ui { class Widget; }
17 QT_END_NAMESPACE
18
19 class Widget : public QWidget
20 {
21 Q_OBJECT
22
23 public:
24 Widget(QWidget *parent = nullptr);
25 ~Widget();
26
27 private slots:
28 void refreshTime();
29 protected:
30 void contextMenuEvent(QContextMenuEvent *event);
31 void mouseMoveEvent(QMouseEvent *event);
32 void mousePressEvent(QMouseEvent *event);
33 void paintEvent(QPaintEvent *event);
34 void mouseDoubleClickEvent(QMouseEvent *event);
35 private:
36 Ui::Widget *ui;
37
38 private:
39 void createActions();
40 QMenu *popMenu;
41 QAction *adjustTimeAction;
42 QAction *adjustDateAction;
43 QAction *quitAction;
44
45 QPoint windowDeskTopLeft; // 窗口左上角相对于桌面的位置
46 QPoint mouseDeskTolLeft; // 鼠标相对于桌面左上角的位置
47 QPoint mouseWindowTopLeft; // 鼠标相对于窗口左上角的位置
48
49 QPoint moveTargetPso; // 鼠标移动坐标
50
51 QPainter paint;
52 };
53 #endif // WIDGET_H
widget.cpp
1 #include "widget.h"
2 #include "ui_widget.h"
3
4 Widget::Widget(QWidget *parent)
5 : QWidget(parent)
6 , ui(new Ui::Widget)
7 {
8 ui->setupUi(this);
9 this->setWindowFlags(Qt::FramelessWindowHint); // 隐藏边框
10 QTimer *timer = new QTimer(this);
11 connect(timer, SIGNAL(timeout()), this, SLOT(refreshTime()));
12 timer->start(1); //1毫秒定时器
13 // timer->setInterval(1);// 间隔1毫秒
14 // timer->start();
15
16 this->createActions();
17 this->setMouseTracking(true); // 实时跟踪鼠标
18 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
19 }
20
21 Widget::~Widget()
22 {
23 delete ui;
24 }
25
26 void Widget::refreshTime()
27 {
28 QTime time = QTime::currentTime(); //读出当前时间
29 QString text = time.toString("hh:mm:ss:zzz"); //格式化当前时间
30 ui->lcdNumber->display(text); //送显示
31 }
32
33 /**
34 * 右键菜单接口
35 * @brief Widget::contextMenuEvent
36 * @param event
37 */
38 void Widget::contextMenuEvent(QContextMenuEvent *event)
39 {
40 qDebug()<<"右键菜单";
41 popMenu->clear(); // 清楚原有菜单
42 popMenu->addAction(adjustTimeAction);
43 popMenu->addAction(adjustDateAction);
44 popMenu->addAction(quitAction);
45 popMenu->exec(QCursor::pos()); // 添加模态对话框
46 }
47
48 /**
49 * 右键菜单结构
50 * @brief Widget::createActions
51 */
52 void Widget::createActions()
53 { // 右键弹出菜单
54 popMenu = new QMenu(this);
55 // 设置popMenu样式表
56 popMenu->setStyleSheet(
57 QString::fromUtf8("background-color: rgb(110, 0, 0);"));
58
59
60 adjustTimeAction=new QAction(this);
61 adjustTimeAction->setText("修改时间");
62
63
64 adjustDateAction=new QAction(this);
65 adjustDateAction->setText("修改日期");
66
67 quitAction=new QAction(this);
68 quitAction->setText(QString::fromLocal8Bit("exit"));
69
70 connect(quitAction,SIGNAL(triggered(bool)),SLOT(close()));
71
72 }
73
74 /**
75 * 鼠标当前坐标 移动窗口
76 * @brief Widget::mouseMoveEvent
77 * @param event
78 */
79 void Widget::mouseMoveEvent(QMouseEvent *event)
80 {
81 QString posStr = QString("%1,%2").arg(event->pos().x()).arg(event->pos().y());
82 QToolTip::showText(QCursor::pos(),posStr,this);
83
84 mouseDeskTolLeft = event->globalPos(); // 鼠标相对于桌面左上角的位置
85 mouseWindowTopLeft = event->pos(); // 鼠标相对于窗口左上角的位置
86 if(event->buttons()& Qt::LeftButton){
87 moveTargetPso = mouseDeskTolLeft - windowDeskTopLeft;
88 qDebug()<<moveTargetPso;
89 this->move(moveTargetPso);
90 }
91 }
92
93 /**
94 * 窗口移动的位置都是相对于桌面左上角
95 * @brief Widget::mousePressEvent
96 * @param event
97 */
98 void Widget::mousePressEvent(QMouseEvent *event)
99 {
100 //也可以是this->geometry().topLeft();
101 windowDeskTopLeft = event->globalPos()-this->pos(); // 窗口左上角相对于桌面的位置
102 }
103
104 /**
105 * 绘制时钟
106 * @brief Widget::paintEvent
107 * @param event
108 */
109 void Widget::paintEvent(QPaintEvent *event)
110 {
111 // 时针分针秒针
112 static const QPoint hourHand[3]={
113 QPoint(7,8),
114 QPoint(-7,8),
115 QPoint(0,-30)
116 };
117 static const QPoint minuteHand[3]={
118 QPoint(7,8),
119 QPoint(-7,8),
120 QPoint(0,-60)
121 };
122 static const QPoint secondHand[3]={
123 QPoint(7,8),
124 QPoint(-7,8),
125 QPoint(0,-90)
126 };
127 QTime time = QTime::currentTime();
128
129 paint.begin(this);
130
131 paint.setRenderHint(QPainter::Antialiasing,true);
132 paint.translate(this->width()/2,this->height()/2); // 移动坐标
133
134 // 缩放
135 int side = qMin(width(),height()); // 长宽的最小值:定义一个边界 保证是圆形的
136 paint.scale(side /200.0,side/200.0); // 跟随窗口大小发生变化
137
138 // 绘制时针刻度线
139 paint.setPen(Qt::white);
140 for (int i =0; i <12; i++){
141 paint.drawLine(QPoint(88,0),QPoint(96,0));
142 paint.rotate(30.0); // 旋转30°
143 }
144
145 // 绘制分针刻度线 (360 / 60 = 6)
146 for(int j =0; j < 60; j++){
147 if((j%5)!=0)
148 {
149 paint.drawLine(92,0,96,0);
150 }
151 paint.rotate(6.0);
152 }
153
154 // 绘制时针 旋转角度 = 小时数 * 30°
155 paint.save();
156 paint.setBrush(Qt::white); // 设置指针填充色
157 paint.rotate(30.0 * (time.hour() + time.minute() /60.0));
158 paint.drawConvexPolygon(hourHand,3);
159 paint.restore(); // 还原画笔
160
161 // 绘制分针 旋转角度 = 分钟数 * 6°
162 paint.save();
163 paint.rotate(6.0 * (time.minute() + time.second() / 60.0));
164 paint.drawConvexPolygon(minuteHand,3);
165 paint.restore(); // 还原画笔
166
167 // 绘制秒针 旋转角度 = 秒数 * 6°
168 paint.save();
169 paint.rotate(6.0 * time.second());
170 paint.drawConvexPolygon(secondHand,3);
171 paint.restore(); // 还原画笔
172
173 paint.end();
174 }
175
176 /**
177 * 鼠标双击全屏
178 * @brief Widget::mouseDoubleClickEvent
179 * @param event
180 */
181 void Widget::mouseDoubleClickEvent(QMouseEvent *event)
182 {
183 static bool fullScreenFlag;
184 if(!fullScreenFlag)
185 {
186 this->showFullScreen();
187 fullScreenFlag = true;
188 }
189 else
190 {
191 this->showNormal();
192 fullScreenFlag = false;
193 }
194 }