Qt時鐘介面、數字時鐘(12小時制24小時制切換、修改系統時間)

洗硯發表於2015-09-13

學習Qt之初參照網上資料,然後稍加修改的時鐘介面,介面包含了時鐘、數字時鐘、12小時制和24小時制相互切換並且可以修改系統時間。

以下程式碼為時鐘部分:

clock.h

#include <QWidget>  
#include <math.h>  
#include <QtGui/QDialog>  
#include <QResizeEvent>  
#include <QGroupBox>  
class Clock : public QWidget  
{  
     Q_OBJECT     
public:  
     Clock(QWidget *parent = 0);  
     void setTime(QTime);  
protected: 
//重繪用的事件處理函式  
     void paintEvent(QPaintEvent *event);  
};   

clock.cpp

#include <QtGui>  
#include "clock.h"  
Clock::Clock(QWidget *parent): QWidget(parent)  
{    //宣告一個定時器  
  QTimer *timer = new QTimer(this);  //連線訊號與槽
  connect(timer, SIGNAL(timeout()), this, SLOT(update()));  
   timer->start(1000);  
   //設定窗體名稱與大小  
   setWindowTitle(tr("Clock"));  
   resize(400,400);  
}  
void Clock::setTime(QTime)    
{     
      
}   
  
void Clock::paintEvent(QPaintEvent *)  
{//刻畫時針、分針、秒針、長刻度、短刻度  
    static const QPoint hourHand[3] = {  
    QPoint(5, 14),  
    QPoint(-5, 14),  
    QPoint(0, -50)  
 };  
    static const QPoint minuteHand[3] = {  
    QPoint(5, 14),  
    QPoint(-5,14),  
    QPoint(0, -70)  
 };  
    static const QPoint secondHand[3] = {  
    QPoint(5, 14),  
    QPoint(-5, 14),  
    QPoint(0, -80)  
 };  
    static const QPoint line_long[2]={  
    QPoint(0,100),  
    QPoint(0,80)  
};  
    static const QPoint line_short[2]={  
    QPoint(0,100),  
    QPoint(0,95)  
};    //繪製的範圍  
   int side = qMin(width(), height()); 
   //獲取當前的時間  
   QTime time = QTime::currentTime();   
   //宣告用來繪圖用的painter    
   QPainter painter(this); 
  //繪製的影像反鋸齒  
   painter.setRenderHint(QPainter::Antialiasing); 
   //重新定位座標起始點,把座標原點放到窗體的中央  
   painter.translate(width() / 2, height() / 2); 
   //在錶盤內新增文字(任意)  
   painter.drawText(-75,-120,100,170,Qt::AlignRight,tr("ROLEX"));  
   //設定畫布的邊界  
   painter.scale(side / 200.0, side / 200.0); 
   //填充邊線設為黑色,也可以設定為NoPen  
   painter.setPen( Qt::black );    
   //畫刷顏色設定  
   painter.setBrush(Qt::black);  
   //儲存painter的狀態  
   painter.save();  
   //設定painter的旋轉角度  
   painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
   //填充時針的區域  
   painter.drawConvexPolygon(hourHand, 3);
   painter.restore();  
   painter.setPen(Qt::black);
   //12個個刻度迴圈  
for (int i = 0; i < 12; ++i)   
{  
    painter.drawLine(line_long[0],line_long[1]);  
    painter.rotate(30.0);  
}  
  //繪製分針轉角、刻度  
   painter.setPen(Qt::black);  
   painter.setBrush(Qt::black);  
  
   painter.save();  
   painter.rotate(6.0 * (time.minute() + time.second() / 60.0));  
   painter.drawConvexPolygon(minuteHand, 3);  
   painter.restore();  
   painter.setPen(Qt::black);  
   for (int j = 0; j < 60; ++j)   
   {  
     if ((j % 5) != 0)  
     painter.drawLine(line_short[0],line_short[1]);  
     painter.rotate(6.0);  
     }  
     //繪製分針轉角、刻度
     painter.setPen(Qt::black);
     painter.setBrush(Qt::black);

      painter.save();
      painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
      painter.drawConvexPolygon(minuteHand, 3);
      painter.restore();
      painter.setPen(Qt::black);
      for (int j = 0; j < 60; ++j) 
        {
            if ((j % 5) != 0)
            painter.drawLine(line_short[0],line_short[1]);
            painter.rotate(6.0);
        }
       //繪製秒針轉角  
       painter.setPen(Qt::black);  
       painter.setBrush(Qt::black);  
  
       painter.save();  
       painter.rotate(6.0*time.second());  
       painter.drawConvexPolygon(secondHand,3);  
       painter.restore(); 
} 

以下程式碼為數字時鐘部分,數字時鐘我做了兩個,鍾一預設24小時制不能調節,鍾二可以上下午的切換變換修改時間:

 

number.h

#include <QLineEdit>    
#include <QLayout>    
#include <QLabel>    
#include <QComboBox>    
#include <QMessageBox>    
#include <QDialog>  
#include <QSystemTrayIcon>    
#include <QLCDNumber>  
#include <QTimeEdit>  
#include <QTimer>  
#include <QTime>  
#include <QVariant>  
#include <QAction>  
#include <qdatetimeedit.h>  
#include <QPushButton>   
#include"clock.h"  
class number:public QLabel  
{  
   Q_OBJECT  
public:  
   number(QWidget *parent=0);  
private:  
   QComboBox *combox;  
   QGridLayout *layout;  
   Clock *shi;  
   QLCDNumber *lcd;//顯示當前時間  
   
   QTimeEdit *timed;  
   QTimer *timer;  
   QTime time;  
   QPushButton *bt;  
public slots:  
   void chang(int);  
   void clockc();  
   void clocks();  
   void satime();  
};  

number.cpp 

 

#include"number.h"  
number::number(QWidget *parent):QLabel(parent)  
{  
   combox=new QComboBox;   
   layout=new QGridLayout;   
   shi= new Clock();  
   lcd=new QLCDNumber;  
   timer=new QTimer;  
   timed=new QTimeEdit;  
   bt=new QPushButton("Save");
   //鍾一  
   lcd->setSegmentStyle(QLCDNumber::Flat);  
   lcd->setNumDigits(8);//顯示八個數字,預設為5個 
   //顯示格式  
   lcd->display(QTime::currentTime().toString("hh:mm:ss"));  //鍾二
     //設定初始顯示的時間和格式    
    time =QTime::currentTime();    
    timed->setDisplayFormat("hh:mm:ss AP");    
    timed->setTime(time);    
    //設定選擇框12小時制和24小時制   
    combox->addItem("12 HOUR");    
    combox->addItem("24HOUR");    
    combox->setFocusPolicy(Qt::StrongFocus);    
    timer->start(1000);
  //簡單佈局
    layout->addWidget(combox,5,3,2,2);
    layout->addWidget(shi,2,7,60,60);  
    layout->addWidget(lcd,8,3,3,3);  
    layout->addWidget(timed,11,3,2,2);  
    layout->addWidget(bt,14,3,2,2);  
    this->setLayout(layout);
    //訊號currentIndexChanged 是在單擊選擇框僅當所選內容前後有變化時才會出發此訊號  
    connect(combox, SIGNAL(currentIndexChanged(int)), this, SLOT(chang(int)));    
    connect(timer, SIGNAL(timeout()), this, SLOT(clockc()));//鍾一  
    connect(timed, SIGNAL(timeChanged(QTime)), this, SLOT(clocks()));//鍾二  
    connect(bt, SIGNAL(clicked()), this, SLOT(satime()));  
}  
void number::chang(int i)  
{//上下午時間的切換  
   switch(i)  
   {  
       case 0:timed->setDisplayFormat(" hh:mm:ss AP"); 
     break;    
      case 1:timed->setDisplayFormat("hh:mm:ss "); 
    break;     
   }  
}  
void number::clockc()  
{  
     lcd->display(QTime::currentTime().toString("hh:mm:ss"));  
     time=time.addSecs(1);//使用addSecs()給一個日期增加一個給定的秒數  
     timed->setTime(time);  
     shi->repaint();  
}  
void number::clocks()  
{   
     time=timed->time();    
     shi->setTime(time);    
    //重新畫時鐘    
     shi->repaint(); 
}  
void number::satime()  
{//修改系統時間  
     QDateTime td=QDateTime::currentDateTime ();  
     td.setTime(timed->time());  
     time_t time=(time_t)td.toTime_t();  
     stime(&time);//校對時間  
}  

main.cpp 

#include <QApplication>  
#include "number.h"
{
  QApplication app(argc, argv);//宣告下,再show出來就可以了
  number q; 
  q.show(); 
  return app.exec(); 
}

效果:

 

 

 

相關文章