参考:http://blog.csdn.net/g457499940/article/details/11923887

---------------------------------------------

From: http://dragoon666.blog.163.com/blog/static/107009194201092602326598/

1.新建Gui工程,在主界面上添加一个标签label,并设置其显示内容为“0000-00-00 00:00:00 星期日”。

2.在mainwindow.h中添加槽函数声明。

private slots:

void timerUpDate();

3.在mainwindow.cpp中添加代码。

添加#include <QtCore>的头文件包含,这样就包含了QtCore下的所有文件。

构造函数里添加代码:

QTimer *timer = new QTimer(this);

//新建定时器

connect(timer,SIGNAL(timeout()),this,SLOT(timerUpDate()));

//关联定时器计满信号和相应的槽函数

timer->start(1000);

//定时器开始计时,其中1000表示1000ms即1秒

4.然后实现更新函数。

void MainWindow::timerUpDate()

{

QDateTime time = QDateTime::currentDateTime();

//获取系统现在的时间

QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");

//设置系统时间显示格式

ui->label->setText(str);

//在标签上显示时间

}

5.运行程序。

======================================================

以下是本人自己整理的代码:

mainwindow.h

 #ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow(); private:
void timerEvent(QTimerEvent *); private:
Ui::MainWindow *ui; private slots:
void on_btnLogin_clicked();
#if 0
void timerUpDate();
#endif
}; #endif // MAINWINDOW_H

mainwindow.cpp

 #include <QMessageBox>
#include <QtCore>
#include <time.h>
#include "mainwindow.h"
#include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
#if 0
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpDate()));
timer->start();
#else
qsrand(time());
startTimer(); // 返回值为1, 即timerId
startTimer(); // 返回值为2
startTimer(); // 返回值为3
#endif
} MainWindow::~MainWindow()
{
delete ui;
} void MainWindow::on_btnLogin_clicked()
{
QMessageBox::information(this, "Caption", tr("Hello你好吗"), QMessageBox::Ok);
} #if 0
void MainWindow::timerUpDate()
{
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->lblCurDate->setText(str);
}
#else
void MainWindow::timerEvent(QTimerEvent *t)
{
switch(t->timerId())
{
case :
{
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->lblCurDate->setText(str);
ui->lbl1->setText(tr("每秒产生一个随机数: %1").arg(qrand() % ));
ui->lbl1->adjustSize();
}
break;
case :
ui->lbl2->setText(tr("5秒后软件将关闭"));
ui->lbl2->adjustSize();
break;
case :
qApp->quit(); // 退出系统
break;
}
} #endif

main.cpp

 #include <QtGui/QApplication>
#include <QTextCodec>
#include "mainwindow.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
MainWindow w;
w.show(); return a.exec();
}   

Qt---在QLabel上实现系统时间的更多相关文章

  1. Qt设置系统时间(使用SetSystemTime API函数)

    大家都知道Qt中有QDateTime等有关时间与日期的类,类中包含很多成员函数,可以很方便的实现有关时间与日期的操作,比如:想要获得系统当前的时间与日期,可以调用currentDateTime();  ...

  2. (C/C++学习)12.获取系统时间制作时钟(system()略解)

    说明:通过调用函数来获取系统当前时间,并制作一个数字式的时钟,时钟的显示包括年.月.日.小时.分以及秒,通过系统屏幕的刷新来对不断更新的时间进行屏幕的显示. 一.对相关函数的学习 1.time_t t ...

  3. [CentOS7服务器] 更改系统时间

    未知何故,服务器上的系统时间不对,比实际的UTC快了将近63分钟.在涉及本地文件与远程服务器文件的时间戳校验时,容易产生混淆. 这里把系统时间更正的过程记录如下. 参考资料:http://www.ce ...

  4. JS显示动态的系统时间--JavaScript基础

    1.网页中实时显示当前时间 <!DOCTYPE html><html lang="en"><head> <meta charset=&qu ...

  5. JS获取系统时间--JavaScript基础

    1.网页中实时显示当前时间 <!DOCTYPE html><html lang="en"><head> <meta charset=&qu ...

  6. 【转】QT获取系统时间,以及设置日期格式

    http://blog.csdn.net/zzk197/article/details/7498593 例如我要在一个label上设置当前时间 QDateTime time = QDateTime:: ...

  7. Java中在java.sql.Date的系统时间上加上30天并写入oracle

    在java.sql.Date的系统时间上加上30天,并写入oracle 思路:通过 Calendar.getInstance() 获得对象,然后 add() 方法添加 时间,再通过 new java. ...

  8. QT在linux环境下读取和设置系统时间(通过system来直接调用Linux命令,注意权限问题)

    QT在Linux环境下读取和设置系统时间 本文博客链接:http://blog.csdn.NET/jdh99,作者:jdh,转载请注明. 环境: 主机:Fedora12 开发软件:QT 读取系统时间 ...

  9. 使用线程 在shell上同步动态显示当前系统时间

    //创建一个用于刷新当前系统时间的线程 new Thread() { public void run() { // 此处为另外一个单独线程,非UI线程 Display dis=shell.getDis ...

随机推荐

  1. PC上安装MAC X Lion

    PC上安装MACXLion 网上关于如何在PC下安装MAC的文章已近不少了,但对于一些初学者在实践当中会遇到各种问题,以下视频资料为大家展示两种虚拟机安装MacOS. 1.VmwareWorkstat ...

  2. SQL Server 索引和视图

    Ø 索引 1. 什么是索引 索引就是数据表中数据和相应的存储位置的列表,利用索引可以提高在表或视图中的查找数据的速度. 2. 索引分类 数据库中索引主要分为两类:聚集索引和非聚集索引.SQL Serv ...

  3. win7发送到菜单

    在文件浏览器地址栏输入 shell:sendto

  4. 嵌入式linux移植LAMP服务器

    一.工具准备工作  要想在ARM平台上移植一套Apache+Msql+PHP的Web型服务器. 所用物理机系统: Ubuntu 14.04 LTS(64位) 交叉编译环境:   arm-linux-g ...

  5. .NET程序员吧需要知道的小知识——关于数据库

    关于数据库 作为一个有“情怀的”(B格高一些的).NET开发工程师,需要多少知道一些这样的小故事. 哪怕仅仅当作一些扯淡的谈资.   1.文件型数据库(常见的) Access SQLite SQLSe ...

  6. What is the difference between differed processing mode and interactive mode?

     Every time you access and navigate through the fields on a page in PeopleSoft there are events such ...

  7. c#自定义控件属性面板及选择资源设置

    博客转移到 http://jacean.github.io/ 继续分享编程经验 因为要做流体布局,但两个控件没办法组合,就做自定义控件.这个控件需要一个text设置文本,一个pic设置图片显示,图片通 ...

  8. [原]打造Python开发环境之Python环境

    人生苦短,我用Python 一.升级到python2.7 开发环境的系统是centos 6.0, 默认的python版本是2.6.6, 由于线上环境是python2.7,为了防止版本差异产生的问题,所 ...

  9. js浏览器各种位置检测

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. ListView的几种形式

    一. ArrayAdapter ListView listView = (ListView) findViewById(R.id.list_view);//ListView的参数为id listVie ...