手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Qt中使用QLabel显示时间的两种方法

[日期:2014-02-27] 来源:Linux社区  作者:Linux [字体:大 中 小]

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

main.cpp

#include "displaytime.h"
#include <QApplication>

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 <QWidget>
#include <QtGui>

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 <QWidget>
#include <QtGui>

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"));
}

Qt 的详细介绍:请点这里
Qt 的下载地址:请点这里

推荐阅读:

Ubuntu 环境下Gtk与QT编译环境安装与配置 http://www.linuxidc.com/Linux/2013-08/88539.htm

Linux系统下QT环境搭建 http://www.linuxidc.com/Linux/2013-07/87576.htm

Ubuntu下QT控制台程序无法运行的解决方案以及XTerm的配置方法 http://www.linuxidc.com/Linux/2013-06/86244.htm

Ubuntu 10.04下QT4.7.4移植详解 http://www.linuxidc.com/Linux/2013-01/77930.htm

Linux下为刚安装好的Qt Creator 创建桌面启动图标 http://www.linuxidc.com/Linux/2012-08/67214.htm

Ubuntu 11.04安装QT Creator 2.5.0 http://www.linuxidc.com/Linux/2012-06/62607.htm

Qt creator for PlayBook安装过程 http://www.linuxidc.com/Linux/2012-05/60652.htm

linux
相关资讯       Qt  Qt QLabel 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款