Qt时钟界面、数字时钟(12小时制24小时制切换、修改系统时间)

学习Qt之初参照网上资料,然后稍加修改的时钟界面,界面包含了时钟、数字时钟、12小时制和24小时制相互切换并且可以修改系统时间。

以下代码为时钟部分:

clock.h

#include   
#include   
#include   
#include   
#include   
class Clock : public QWidget  
{  
     Q_OBJECT     
public:  
     Clock(QWidget *parent = 0);  
     void setTime(QTime);  
protected: 
//重绘用的事件处理函式  
     void paintEvent(QPaintEvent *event);  
};   

clock.cpp

#include   
#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     
#include     
#include     
#include     
#include     
#include   
#include     
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include    
#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   
#include "number.h"
{
  QApplication app(argc, argv);//声明下,再show出来就可以了
  number q; 
  q.show(); 
  return app.exec(); 
}

效果:

Qt时钟界面、数字时钟(12小时制24小时制切换、修改系统时间)_第1张图片

 

 

 

你可能感兴趣的:(Qt,QTimer,QPainter,时钟,电子钟)