设为首页 加入收藏

TOP

Qt中使用QLabel显示时间的两种方法
2014-11-24 01:09:10 】 浏览:4196
Tags:使用 QLabel 显示 时间 方法

Qt中使用QLabel显示时间的两种方法思路一致,只是实现方法不一样而已。


main.cpp


#include "displaytime.h"
#include


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DisplayTime w;
w.show();

return a.exec();
}


方法一:


displaytime.h


#ifndef DISPLAYTIME_H
#define DISPLAYTIME_H


#include
#include


class QLabel;


class DisplayTime : public QWidget
{
Q_OBJECT

public:
DisplayTime(QWidget *parent = 0);
~DisplayTime();


private:
QLabel *timeLabel;


protected:
void timerEvent(QTimerEvent * event);
};


#endif // DISPLAYTIME_H


displaytime.cpp


#include "displaytime.h"


DisplayTime::DisplayTime(QWidget *parent)
: QWidget(parent)
{
timeLabel = new QLabel(this);
timerEvent(0);
startTimer(1000);
timeLabel->show();
}


DisplayTime::~DisplayTime()
{

}


void DisplayTime::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
timeLabel->setText(QTime::currentTime().toString("hh:mm:ss"));
}


方法二:


displaytime.h


#ifndef DISPLAYTIME_H
#define DISPLAYTIME_H


#include
#include


class QLabel;


class DisplayTime : public QWidget
{
Q_OBJECT

public:
DisplayTime(QWidget *parent = 0);
~DisplayTime();


private:
QLabel *timeLabel;


private slots:
void updateTime();
};


#endif // DISPLAYTIME_H


displaytime.cpp


#include "displaytime.h"


DisplayTime::DisplayTime(QWidget *parent)
: QWidget(parent)
{
timeLabel = new QLabel(this);
timeLabel->setGeometry(0, 0, 150, 30);


QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()),
this, SLOT(updateTime()));
timer->start(1000);


timeLabel->show();
}


DisplayTime::~DisplayTime()
{

}


void DisplayTime::updateTime()
{
timeLabel->setText(QDateTime::currentDateTime().toString("hh:mm:ss"));
}


推荐阅读:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linux下armcc和arm-linux交叉编译.. 下一篇Android 使用ViewPager实现左右循..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目