当前位置:网站首页>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();
}

 

 

原网站

版权声明
本文为[PureヾChan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210545036715.html