当前位置:网站首页>QT electronic clock
QT electronic clock
2022-06-25 01:02:00 【PureヾChan】
Get to know more about Qt Properties in , Now let's do another little exercise , To achieve the effect of a simple electronic clock .
Effect display :
New projects :
We created lcdclock2 class , In fact, it is not used , It's actually adding a new one C++ File to realize electronic clock .
add to C++ file :
Code :
clock.h
#ifndef CLOCK_H
#define CLOCK_H
#include <QDialog>
class clock : public QDialog
{
Q_OBJECT
public:
clock(QWidget *parent = 0);
~clock();
};
#endif // CLOCK_H
This clock Documents are of little use , In fact, it exists to create dialog boxes .
digitalclock.h
#ifndef DIGITALCLOCK_H
#define DIGITALCLOCK_H
#include<QLCDNumber>
class digitalclock : public QLCDNumber
{
Q_OBJECT
public:
digitalclock(QWidget* parent=0);
protected:
void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
private slots:
void showTime();
private:
QPoint m_dragPosition;
bool m_showColon;
};
#endif // DIGITALCLOCK_H
Added digitalclock Header file add header file QLCDNumber, Adding a meta object system , This is necessary . Then you need to rewrite the mouse click event mousePressEvent also mouseMoveEvent Mouse movement events . Write another slot function to display the time , Is to print the time into the dialog box . There are two private variables. One is m_dragPosition Used to save the position distance from the mouse click point to the upper left of the window . Another variable is bool type m_showColon Used to save the status of colon , The stroboscopic state is reached .
clock.cpp
#include "clock.h"
clock::clock(QWidget *parent)
: QDialog(parent)
{
}
clock::~clock()
{
}
digitalclock.cpp
#include "digitalclock.h"
#include<QMouseEvent>
#include<QDebug>
#include<QTime>
#include<QTimer>
digitalclock::digitalclock(QWidget* parent):QLCDNumber(parent)
{
// Set a blue background
QPalette p=palette();//palette Function returns a palette
p.setColor(QPalette::Window,Qt::blue);
setPalette(p);// Set the acquired palette to the color of the form
// Format border
setWindowFlags(Qt::FramelessWindowHint);
// Set the transparency of the form
setWindowOpacity(0.5);
QTimer* timer=new QTimer(this);
connect(timer,&QTimer::timeout,this,&digitalclock::showTime);
timer->start(1000);// Set timer cycle
showTime();
resize(200,100);
m_showColon=true;
}
void digitalclock::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton){
m_dragPosition=event->globalPos()-frameGeometry().topLeft();
qDebug()<<" Global coordinates :"<<event->globalPos();
qDebug()<<" Top left coordinates "<<frameGeometry().topLeft();
event->accept();
}else{
close();// close window
}
}
void digitalclock::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons()==Qt::LeftButton){
move(event->globalPos()-m_dragPosition);
// Move the electronic clock ,move The function actually moves around with the upper left corner of the window as the focus , If you do not subtract the offset value of the mouse and the upper left corner , The window automatically offsets
event->accept();
}
}
void digitalclock::showTime()
{
QTime time=QTime::currentTime();// Define a time object , Gets the current time
QString strTime=time.toString("hh:mm");// Turn time into a string , And specify the format
if(m_showColon){
strTime[2]=':';
}else{
strTime[2]=' ';
}
display(strTime);
m_showColon=!m_showColon;// Set the display status of the colon to be reversed , In this way, we can achieve the appearance of stroboscopic
}
Constructors :
First define a palette object , utilize palette() Function returns a palette . Using palette functions setColror( Color type , Color ); Set the form style function to use setWIndowFlags(); The set form transparency function is setWIndowOpacity function . Then define a timer QTimer. Then connect the timer to showTIme Slot function , The slot function is called whenever the timer time expires , Use it later resize Function to resize the window . First set the colon display status to true .
Mouse click function mousePreessEvent function :
First, determine whether the mouse is left click , If so, calculate the distance between the point clicked by the mouse and the upper left corner of the window , Save in m_dragposition. Always bring... At the end of the function event.accept Accept function modification .
Mouse movement events mouseMoveEvent function :
First, judge whether the left mouse button is clicked , Because usually , The mouse is moved by pressing the left button ,move The function actually focuses on the top left of the window , If we simply transfer the global coordinates of the mouse , In this way, the top left of the window will jump to the click point of the mouse . It doesn't look good , So when you transmit coordinates , It is the point clicked by the mouse minus the offset position of the upper left side of the window relative to the mouse down .
Time display function showTIme function
Time display function is to define a time object , utilize QTime::Current Function to get the current time . When converting the time to the specified format string , Then judge the display status of colon , Set the colon to yes or no according to the status . The real function of time is display function , Print the time string into the window . Then reverse the colon status , Reach every second of stroboscopic .
mian.cpp
#include "clock.h"
#include <QApplication>
#include"digitalclock.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
digitalclock w;
w.show();
return a.exec();
}
边栏推荐
- C# 闭包的垃圾回收
- QT (35) - operate excel qxlsx qaxobject
- Decoupling pages and components using lifecycle
- Garbage collection of C closure
- If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?
- Leetcode 1248. 统计「优美子数组」(害,突然发现只会暴力枚举了)
- Mobile security tool -dex2jar
- 移动安全工具-jar
- Hyperledger Fabric 2. X dynamic update smart contract
- Helm chart仓库操作
猜你喜欢
【微服务|Sentinel】实时监控|RT|吞吐量|并发数|QPS
Sliding window technology based on byte in transport layer
2022安全员-C证考试模拟100题及在线模拟考试
生成订单30分钟未支付,则自动取消,该怎么实现?
傳輸層 以字節為單比特的滑動窗口技術
Examination questions and mock examination for safety management personnel of hazardous chemical business units in 2022
Simulation questions and answers of the latest national fire facility operator (senior fire facility operator) in 2022
Text editor for QT project practice - Episode 12
Virtual machine - network configuration
Custom animation (simulated win10 loading animation) - Optimization
随机推荐
[redis realizes seckill service ②] solution to oversold problem
Custom animation (simulated win10 loading animation)
Helm chart仓库操作
通过kubernetes可视化界面(rancher)安装kibana
Sanic服务启动失败
2021-04-18
2021-09-12
Thermodynamic diagram display correlation matrix
C#和C 的CAN通信实验
Uniapp encapsulated incentive advertisement, screen insert advertisement and banner advertisement
[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS
2022 melting welding and thermal cutting recurrent training question bank simulated examination platform operation
Golang示例续期锁:Redis+Channel+sync.Mutex
Simple collation of Web cache
If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?
2022r1 quick opening pressure vessel operation test questions and answers
Databinding quick start (still using findviewbyid?)
Usage of assert
Qiniu cloud uploads video to get the first frame of video
我想问一下兴业证券怎么开户?通过链接办理股票开户安全吗