您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:Qt5 实现主窗口状态栏显示时间

51自学网 2021-10-30 22:48:58
  python
这篇教程Qt5 实现主窗口状态栏显示时间写得很实用,希望能帮到您。

使用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_OBJECTpublic:  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的文本内容为时间}

补充: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);}

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


PyQt5 实现状态栏永久显示消息
python 模拟网站登录——滑块验证码的识别
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。