QT时钟的创建

来源:互联网 发布:centos无线网络配置 编辑:程序博客网 时间:2024/05/20 19:46

       1.QLCDNumber的使用

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QTime>
#include <QTimer>
#include <QMessageBox>
#include <QtGui/QLCDNumber>

namespace Ui {
class Logo;
}

class  Logo: public QMainWindow
{
    Q_OBJECT
   
public:
    explicit Logo(QWidget *parent = 0);
    ~Logo();
    QLCDNumber *LCD;
private slots:              //关键字
    void showTime();
   
private:
    Ui::Logo *ui;
    QTimer *timer;
};

#endif // MAINWINDOW_H

#include "Logo.h"
#include "ui_mainwindow.h"

Logo::Logo(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Logo)
{
    ui->setupUi(this);

    Qt::WindowFlags new_flags=0;
        new_flags = Qt::Window|Qt::FramelessWindowHint;
        setWindowFlags(new_flags);
        setGeometry(0,0,480,272);

        QPixmap background;
        background = QPixmap("D:/QtSDK/QtCreator/NewProject/jiemian.jpg");

        QPalette palette;
        palette.setBrush(QWidget::backgroundRole(),QBrush(background));
        setPalette(palette);

        timer = new QTimer(this);
       // timer->start(2000);
        connect(timer,SIGNAL(timeout()),this,SLOT(MyLOGO));
    LCD = new QLCDNumber(this);
    LCD->setSegmentStyle(QLCDNumber::Filled);
    LCD->setNumDigits(8);//设置显示数字位数
    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this, SLOT(showTime()));
    timer->start(1000);
    showTime();
    setWindowTitle(tr("Digital Clock"));
    LCD->resize(150,60);
}

Logo::~Logo()
{
    delete ui;
}

void Logo::showTime()
{
     QTime time=QTime::currentTime();
     QString text=time.toString("hh:mm:ss");
     if((time.second()%2)==0)
     {
         text[2]=' ';
         text[5]=' ';
     }
     LCD->display(text);
}

2.图片方式显示

 

void login::showdate()
{
        QDate date=QDate::currentDate();
        QString text_date = date.toString("yyyy/MM/dd  ddd");
       // label_date->setText(text_date);
        ui->yearh1->setPixmap(QPixmap(getIconname(text_date[0])));
        ui->year2->setPixmap(QPixmap(getIconname(text_date[1])));
        ui->year3->setPixmap(QPixmap(getIconname(text_date[2])));
        ui->year4->setPixmap(QPixmap(getIconname(text_date[3])));
     //   ui->xie->setPixmap(QPixmap(getIconname(text_date[4])));
        ui->month1->setPixmap(QPixmap(getIconname(text_date[5])));
        ui->month2->setPixmap(QPixmap(getIconname(text_date[6])));
      //  ui->xie1->setPixmap(QPixmap(getIconname(text_date[7])));
        ui->day1->setPixmap(QPixmap(getIconname(text_date[8])));
        ui->day2->setPixmap(QPixmap(getIconname(text_date[9])));
}

void login::showtime()
{
        QTime time=QTime::currentTime();
        QString text = time.toString("HH:mm:ss a");
        ui->hourh->setPixmap(QPixmap(getIconname(text[0])));
        ui->hourl->setPixmap(QPixmap(getIconname(text[1])));
      //  ui->mouhao->setPixmap(QPixmap(getIconname(text[2])));
        ui->minh->setPixmap(QPixmap(getIconname(text[3])));
        ui->minl->setPixmap(QPixmap(getIconname(text[4])));
       // ui->maohao1->setPixmap(QPixmap(getIconname(text[5])));
        ui->sech->setPixmap(QPixmap(getIconname(text[6])));
        ui->secl->setPixmap(QPixmap(getIconname(text[7])));
        //label_time->setText(text_time);
}

QString login::getIconname(QChar x)
{
    return (QString(":/file/images/")+x+QString(".gif"));
}