Implementation of Qt-LCD electronic clock and qt-LCD electronic clock

Source: Internet
Author: User

Implementation of Qt-LCD electronic clock and qt-LCD electronic clock

First.

In this way, a simple time is displayed.

Note that we 'd better create an empty file. here we need to create a class that integrates QLCDNumber.

The specific method is as follows:

Source code

Digiclock. h

#ifndef DIGICLOCK_H#define DIGICLOCK_H#include 
 
  #include 
  
   class DigiClock : public QLCDNumber{    Q_OBJECTpublic:    DigiClock(QWidget *parent = 0);    void mousePressEvent(QMouseEvent *event);    void mouseMoveEvent(QMouseEvent *event);private slots:    void showTime();private:    QPoint dragPosition;    bool showColon;};#endif // DIGICLOCK_H
  
 

Digiclock. cpp

#include "digiclock.h"#include 
 
  #include 
  
   #include 
   
    #include 
    
     DigiClock::DigiClock(QWidget *parent) : QLCDNumber(parent){    QPalette p = palette();    p.setColor(QPalette::Window,Qt::blue);    setPalette(p);    setWindowFlags(Qt::FramelessWindowHint);    setWindowOpacity(0.5);    QTimer *timer = new QTimer(this);    connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));    timer->start(1000);    showTime();    resize(150,60);    showColon = true;}void DigiClock::mousePressEvent(QMouseEvent *event){    if(event->button() == Qt::LeftButton)    {        dragPosition = event->globalPos()-frameGeometry().topLeft();        event->accept();    }    if(event->button() == Qt::RightButton)    {        close();    }}void DigiClock::mouseMoveEvent(QMouseEvent *event){    if(event->buttons() & Qt::LeftButton)    {        move(event->globalPos() - dragPosition);        event->accept();    }}void DigiClock::showTime(){    QTime time = QTime::currentTime();    QString text = time.toString("hh:mm");    if(showColon)    {        text[2] = ':';        showColon = false;    }    else    {        text[2] = ' ';        showColon = true;    }    display(text);}
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.