QT實現動態顯示系統時間

希望望天發表於2016-12-19

首先,需要建立一個類,繼承於Qwidget,用於實現時間的獲取介面。然後,在widget建構函式中new一個該類的物件,(widget類可以專門用來顯示背景圖片,通過重寫paintevent()事件)則這個時間就顯示到介面了。

建立該類,自定義名字為Main_Widget:

widget.h:

//類Main_Widget
//顯示時間

#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
#include <QtGui/QDateTimeEdit>
#include <QPushButton>
#include <QLabel>
#include <QTimer>
#include <QTextBrowser>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QSpacerItem>
#include <QPalette>
#include <QPainter>
#include <QtGui/QWidget>
#include <QMessageBox>
#include <QLineEdit>
#include <QSignalMapper>
#include <QTextCodec>
#include <QComboBox>
#include <QTableWidget>
#include <QToolButton>
class Main_Widget : public QWidget
{
    Q_OBJECT


public:


   Main_Widget(QWidget *parent = 0);
   ~Main_Widget();
private:

    QString date_string;
    QString week_string;
    QString time_string;


    QLabel *date_label;
    QLabel *week_label;
    QLabel *time_label;
    //定時器
    QTimer * display_timer;


    QLabel *display_Image;
    QLabel *display_name;


private slots:
    void Update_Time(void);
};


Main_Widget.cpp:

#include <QDebug>


#include "widget.h"
#include <QImage>


Main_Widget::Main_Widget(QWidget *parent)
    : QWidget(parent)
{
    resize(1024,600);   //set interface size
    date_label = new QLabel(this);
    week_label = new QLabel(this);
    time_label = new QLabel(this);
    //設定時間顯示的大小


    date_label->setParent(this);
    display_timer = new QTimer(this);


    connect(display_timer,SIGNAL(timeout()),this,SLOT(Update_Time()));


    display_timer->start(1000);      //after 1 S,restart Timer


    date_label->setGeometry(20,100,200,50);
    date_label->setStyleSheet("color:rgb(0, 0, 135);font-size:30px");
    time_label->setGeometry(20,150,200,50);
    time_label->setStyleSheet("color:rgb(0, 0, 135);font-size:50px");
    week_label->setGeometry(20,200,200,50);
    week_label->setStyleSheet("color:rgb(0, 0, 135);font-size:30px");


    Update_Time();
}

//類Widget   //只負責顯示背景
class Widget : public QWidget
{
    Q_OBJECT


public:

    Widget(QWidget *parent = 0);
    ~Widget();


private:

    Main_Widget * myMain_Widget;

protected:
    void paintEvent(QPaintEvent *event);


};

#endif // WIDGET_H






//
void Main_Widget::Update_Time(void)
{
//    time_label->clear();
    QDateTime Current_time = QDateTime::currentDateTime();
//    QString String_time = Current_time.toString("yyyy-MM-dd hh:mm:ss dddd");
    date_string = Current_time.toString("yyyy-MM-dd");
    qDebug()<<"date = "<<date_string;
//    date_string = Current_time.toString("dd");
//    qDebug()<<"date2= "<<date_string.toInt();
    week_string = Current_time.toString("dddd");
//    time_string = Current_time.toString("hh:mm:ss");
    time_string = Current_time.toString("hh:mm");
    date_label->setText(date_string);
    week_label->setText(week_string);
    time_label->setText(time_string);
//    qDebug()<<time_label->text();


}


Main_Widget::~Main_Widget()
{
    delete date_label;
    delete week_label;
    delete time_label;
    delete display_timer;
}


widget.cpp:

#include "widget.h"

//只是負責主介面的背景


Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle("Main Interface");
    //設定視窗無邊框
    setWindowFlags(Qt::FramelessWindowHint);
    resize(1024,600);   //set interface size


    myMain_Widget = new Main_Widget(this);

}


//重寫了paintEvent()事件
void Widget::paintEvent(QPaintEvent * event)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,this->width(),this->height(),QPixmap(":/Images/3.jpg"));
}

Widget::~Widget()
{
    delete myMain_Widget;

}

主函式:



//主函式
#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{


    QApplication a(argc, argv);


    Widget myWidget;
    myWidget.show();


    return a.exec();
}



相關文章