Qt5 实现主窗口状态栏显示时间

作者:落叶_小唱 时间:2022-05-29 23:54:45 

使用Qt Creator创建默认的窗体程序后,主窗口QMainWindow有statusBar状态栏,在此状态栏实时显示时间可以使用下面方法实现:

mainwindow.h文件内容:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <mydialog.h>
#include <QLabel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
 Q_OBJECT
public:
 explicit MainWindow(QWidget *parent = 0);
 ~MainWindow();
private slots:
 void on_actionNew_Window_triggered();
 void time_update(); //时间更新槽函数,状态栏显示时间
private:
 Ui::MainWindow *ui;
 QLabel *currentTimeLabel; // 先创建一个QLabel对象
 MyDialog *mydialog;
};
#endif // MAINWINDOW_H

mainwindow.c文件内容:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mydialog.h"
#include <QLabel>
#include <QDateTime>
#include <QTimer>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
 QMainWindow(parent),
 ui(new Ui::MainWindow)
{
 ui->setupUi(this);
 currentTimeLabel = new QLabel; // 创建QLabel控件
 ui->statusBar->addWidget(currentTimeLabel); //在状态栏添加此控件
 QTimer *timer = new QTimer(this);
 timer->start(1000); //每隔1000ms发送timeout的信号
 connect(timer, SIGNAL(timeout()),this,SLOT(time_update()));
}
MainWindow::~MainWindow()
{
 delete ui;
}
void MainWindow::on_actionNew_Window_triggered()
{
 mydialog = new MyDialog;
 mydialog->show();
}
void MainWindow::time_update()
{
 //[1] 获取时间
 QDateTime current_time = QDateTime::currentDateTime();
 QString timestr = current_time.toString( "yyyy年MM月dd日 hh:mm:ss"); //设置显示的格式
 currentTimeLabel->setText(timestr); //设置label的文本内容为时间
}

Qt5 实现主窗口状态栏显示时间

补充:Qt 通过QLabel控件来显示实时日期时间

头文件需添加:


#include <QTimer>

构造函数中:


//日期/时间显示
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
timer->start(1000);

定义成员函数timerUpdate()实现用户界面显示时间:


void userwindow::timerUpdate()
{
 QDateTime time = QDateTime::currentDateTime();
 QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
 ui->dateTime->setText(str);
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/ouening/article/details/83026779

标签:Qt5,主窗口,状态栏,时间
0
投稿

猜你喜欢

  • Sun正式发布MySQL 5.1版 简化数据库应用

    2008-12-11 15:15:00
  • python爬虫爬取淘宝商品比价(附淘宝反爬虫机制解决小办法)

    2021-11-14 06:16:40
  • python yield迭代器详解

    2023-12-06 09:36:50
  • Dreamweaver快速编辑网页标签

    2009-05-29 18:35:00
  • 13个超级有用的 jQuery 内容滚动插件和教程

    2011-08-10 19:10:08
  • Go语言题解LeetCode1266访问所有点的最小时间示例

    2023-08-29 08:10:39
  • php二分查找二种实现示例

    2023-11-21 00:40:13
  • 用python的哈希函数对密码加密

    2021-06-10 21:02:58
  • Python实现冒泡排序算法的完整实例

    2021-03-04 10:47:33
  • MySQL的性能调优工具:比mysqlreport更方便的tuning-primer.sh

    2008-12-08 08:37:00
  • JS 显示当前日期+星期(静态)

    2007-09-11 13:29:00
  • Python实现按照指定要求逆序输出一个数字的方法

    2023-12-21 23:37:46
  • 重新认识ASP之后的我在想"是时候改变了"

    2008-05-08 13:10:00
  • 用Python做一个哔站小姐姐词云跳舞视频

    2022-09-17 12:32:30
  • 像表格一样用DIV+CSS给网页布局

    2008-10-18 15:45:00
  • python如何给内存和cpu使用量设置限制

    2021-03-04 00:43:00
  • 再谈float菜单局中

    2009-12-21 19:57:00
  • Python机器学习之scikit-learn库中KNN算法的封装与使用方法

    2021-04-05 15:32:40
  • Python+OpenCV让电脑帮你玩微信跳一跳

    2021-12-16 10:31:17
  • 解决oracle用户连接失败的解决方法

    2011-01-04 19:35:00
  • asp之家 网络编程 m.aspxhome.com