您的位置:首页 > 编程语言 > Qt开发

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

2015-09-13 18:18 531 查看

学习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)
{ <pre class="html" name="code"> //声明一个定时器
QTimer *timer = new QTimer(this); <pre class="html" name="code"> //连接信号与槽
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)
};  <pre class="html" name="code">   //绘制的范围
int side = qMin(width(), height());  <pre class="html" name="code">   //获取当前的时间
QTime time = QTime::currentTime(); <pre class="html" name="code">   //声明用来绘图用的painter
QPainter painter(this);<pre class="html" name="code">   //绘制的图像反锯齿
painter.setRenderHint(QPainter::Antialiasing); <pre class="html" name="code">   //重新定位坐标起始点,把坐标原点放到窗体的中央
painter.translate(width() / 2, height() / 2);<pre class="html" name="code">   //在表盘内添加文字(任意)
painter.drawText(-75,-120,100,170,Qt::AlignRight,tr("ROLEX"));
//设定画布的边界
painter.scale(side / 200.0, side / 200.0);<pre class="html" name="code">   //填充边线设为黑色,也可以设置为NoPen
painter.setPen( Qt::black );<pre class="html" name="code">   //画刷颜色设定
painter.setBrush(Qt::black);
//保存painter的状态
painter.save();  <pre class="html" name="code">   //设置painter的旋转角度
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));<pre class="html" name="code">   //填充时针的区域
painter.drawConvexPolygon(hourHand, 3);  <pre class="html" name="code">   painter.restore();
painter.setPen(Qt::black);<pre class="html" name="code">   //12个个刻度循环
for (int i = 0; i < 12; ++i)
{
painter.drawLine(line_long[0],line_long[1]);
painter.rotate(30.0);
}  <pre class="html" name="code">//绘制分针转角、刻度
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);
}<pre class="html" name="code">      //绘制秒针转角
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");<pre class="html" name="code"> //钟一
lcd->setSegmentStyle(QLCDNumber::Flat);
lcd->setNumDigits(8);//显示八个数字,默认为5个 <pre class="html" name="code"> //显示格式
lcd->display(QTime::currentTime().toString("hh:mm:ss")); <pre class="html" name="code"> //钟二
//设置初始显示的时间和格式
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);<pre class="html" name="code">    //简单布局<pre class="html" name="code">    layout->addWidget(combox,5,3,2,2);<pre class="html" name="code">    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);<pre class="html" name="code">   //信号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"); <pre class="html" name="code">break;
case 1:timed->setDisplayFormat("hh:mm:ss "); <pre class="html" name="code">  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(); <pre class="html" name="code">}
void number::satime()
{//修改系统时间
QDateTime td=QDateTime::currentDateTime ();
td.setTime(timed->time());
time_t time=(time_t)td.toTime_t();
stime(&time);//校对时间
}
}
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"<pre class="html" name="code">{<pre class="html" name="code"> QApplication app(argc, argv);//声明下,再show出来就可以了<pre class="html" name="code"> number q; <pre class="html" name="code"> q.show(); <pre class="html" name="code"> return app.exec(); <pre class="html" name="code">}








效果:

</pre><img src="https://img-blog.csdn.net/20150913183252284" alt="" /></p><p><pre class="html" name="code">

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: