Qt---在QLabel上实现系统时间
参考: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上实现系统时间的更多相关文章
- Qt设置系统时间(使用SetSystemTime API函数)
大家都知道Qt中有QDateTime等有关时间与日期的类,类中包含很多成员函数,可以很方便的实现有关时间与日期的操作,比如:想要获得系统当前的时间与日期,可以调用currentDateTime(); ...
- (C/C++学习)12.获取系统时间制作时钟(system()略解)
说明:通过调用函数来获取系统当前时间,并制作一个数字式的时钟,时钟的显示包括年.月.日.小时.分以及秒,通过系统屏幕的刷新来对不断更新的时间进行屏幕的显示. 一.对相关函数的学习 1.time_t t ...
- [CentOS7服务器] 更改系统时间
未知何故,服务器上的系统时间不对,比实际的UTC快了将近63分钟.在涉及本地文件与远程服务器文件的时间戳校验时,容易产生混淆. 这里把系统时间更正的过程记录如下. 参考资料:http://www.ce ...
- JS显示动态的系统时间--JavaScript基础
1.网页中实时显示当前时间 <!DOCTYPE html><html lang="en"><head> <meta charset=&qu ...
- JS获取系统时间--JavaScript基础
1.网页中实时显示当前时间 <!DOCTYPE html><html lang="en"><head> <meta charset=&qu ...
- 【转】QT获取系统时间,以及设置日期格式
http://blog.csdn.net/zzk197/article/details/7498593 例如我要在一个label上设置当前时间 QDateTime time = QDateTime:: ...
- Java中在java.sql.Date的系统时间上加上30天并写入oracle
在java.sql.Date的系统时间上加上30天,并写入oracle 思路:通过 Calendar.getInstance() 获得对象,然后 add() 方法添加 时间,再通过 new java. ...
- QT在linux环境下读取和设置系统时间(通过system来直接调用Linux命令,注意权限问题)
QT在Linux环境下读取和设置系统时间 本文博客链接:http://blog.csdn.NET/jdh99,作者:jdh,转载请注明. 环境: 主机:Fedora12 开发软件:QT 读取系统时间 ...
- 使用线程 在shell上同步动态显示当前系统时间
//创建一个用于刷新当前系统时间的线程 new Thread() { public void run() { // 此处为另外一个单独线程,非UI线程 Display dis=shell.getDis ...
随机推荐
- 【spring 4】AOP:动态代理
一.动态代理简介 动态代理与普通代理相比较,最大的好处是接口中声明的所有方法都被转移到一个集中的方法中处理(invoke),这样,在接口方法数量比较多的时候,我们可以进行灵活处理,而不需要像静态代理那 ...
- WF4 常用类<第二篇>
一.WorkflowInvoker 常用方法如下: 方法 说明 BeginInvoke() 使用指定的 AsyncCallback 和用户提供的状态以异步方式调用工作流 EndInvoke() 返回使 ...
- Recover deleted pictures in iOS 9
A case about business secret. The suspect is an engineer in Hitec company, and compeitiors pay lots ...
- VMware NAT模式 Cent OS IP配置
1:首先VMware 桥接模式 CentOS ip 配置,关键点,ip的网关和DNS1设置成宿主机的网关和DNS 原理:桥接的模式就是通过物理网卡实现的. 2:以图展示VMware NAT模式 Cen ...
- leetcode 1
题目: 最开始采用暴力解法,两个for循环遍历所有组合形式,时间复杂度为O(n2),代码省略. 进一步学习,采用hash表存储,空间换时间,时间复杂度为O(n),空间复杂度为O(n); 将数组放入ha ...
- Android IOS WebRTC 音视频开发总结(六四)-- webrtc能走多远我不知道,但这个市场真实存在
本文主要总结目前都有哪些使用场景用到webrtc,文章最早发表在我们的微信公众号上,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com webrtc只 ...
- PHP出错界面详细说明
在web 上所有常见的错误之一就是无效的链接.一旦从其它站点上出现了无效的链接,你会重新安排你 的站点.人们会将他们喜欢的站点存成书签,如果在三个月后再次访问时,仅仅发现的是'404 Not Foun ...
- Ubuntu14.04进入文本模式方法
ubuntu 10.10以前的操作方法: 1 第一步,具体命令及操作如下: sudo vi /etc/init/rc-sysinit.conf env DEFAULT_RUNLEVEL=3 <- ...
- c# winfrom应用程序关闭任务管理器中的进程
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- LINUX下网站压力测试工具webbench
wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar.gz cd we ...