Qt---在QLabel上实现系统时间
2015-11-18 16:35
465 查看
参考:/article/2073426.html
---------------------------------------------
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
mainwindow.cpp
main.cpp
---------------------------------------------
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 = 0); ~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(1000); #else qsrand(time(0)); startTimer(1000); // 返回值为1, 即timerId startTimer(5000); // 返回值为2 startTimer(10000); // 返回值为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 1: { 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() % 10)); ui->lbl1->adjustSize(); } break; case 2: ui->lbl2->setText(tr("5秒后软件将关闭")); ui->lbl2->adjustSize(); break; case 3: 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(); }
相关文章推荐
- Ⅰ.2.6.6 如何测试Graphics Views、Graphics Scenes 和 Graphics Items (Qt 4)
- Ⅰ.2.6.5 如何测试QAction、QMenu、以及QMenuBar(Qt 4)
- Qt ini配置文件读写
- Qt的进度条设置
- 在Ubuntu 10.04中文版下Qt编程,使用unixODBC和FreeTDS连接MS SQL Server 2005,并且中文不出现乱码的方法
- QT中在全盘中寻找特定的文件
- QT在VS2010下的使用配置
- Ⅰ.2.6.4 如何测试 Table Widgets,以及使用外部数据文件(Qt 4)
- Qt和WPF比较---控件布局和消息响应
- 用Qt写的一个仿Windows记事本程序
- QT生成PDF文件
- qt处理浏览器问题
- Ⅰ.2.6.3.6 如何测试 QTreeViews(QAbstractItemModels 和 QItemSelectionModels)中的 Items
- QT5中处理windows系统消息QAbstractNativeEventFilter
- VS IDE 下编译Qt 源码
- Ubuntu 15.04 安装配置 Qt + SQLite3
- Ubuntu 15.04 安装配置 Qt + SQLite3
- qt相关界面 博客 很好
- qt
- Ⅰ.2.6.3.5 如何测试 QTreeWidgets 中的 Items