Qt5 實現(xiàn)主窗口狀態(tài)欄顯示時間
使用Qt Creator創(chuàng)建默認(rèn)的窗體程序后,主窗口QMainWindow有statusBar狀態(tài)欄,在此狀態(tài)欄實時顯示時間可以使用下面方法實現(xiàn):
mainwindow.h文件內(nèi)容:
#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(); //時間更新槽函數(shù),狀態(tài)欄顯示時間
private:
Ui::MainWindow *ui;
QLabel *currentTimeLabel; // 先創(chuàng)建一個QLabel對象
MyDialog *mydialog;
};
#endif // MAINWINDOW_H
mainwindow.c文件內(nèi)容:
#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; // 創(chuàng)建QLabel控件
ui->statusBar->addWidget(currentTimeLabel); //在狀態(tài)欄添加此控件
QTimer *timer = new QTimer(this);
timer->start(1000); //每隔1000ms發(fā)送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"); //設(shè)置顯示的格式
currentTimeLabel->setText(timestr); //設(shè)置label的文本內(nèi)容為時間
}

補充:Qt 通過QLabel控件來顯示實時日期時間
頭文件需添加:
#include <QTimer>
構(gòu)造函數(shù)中:
//日期/時間顯示 QTimer *timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate())); timer->start(1000);
定義成員函數(shù)timerUpdate()實現(xiàn)用戶界面顯示時間:
void userwindow::timerUpdate()
{
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->dateTime->setText(str);
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
python3+PyQt5 數(shù)據(jù)庫編程--增刪改實例
今天小編就為大家分享一篇python3+PyQt5 數(shù)據(jù)庫編程--增刪改實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
利用Python?NumPy庫及Matplotlib庫繪制數(shù)學(xué)函數(shù)圖像
最近開始學(xué)習(xí)數(shù)學(xué)了,有一些題目的函數(shù)圖像非常有特點,下面這篇文章主要給大家介紹了關(guān)于利用Python?NumPy庫及Matplotlib庫繪制數(shù)學(xué)函數(shù)圖像的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Python面試不修改數(shù)組找出重復(fù)的數(shù)字
這篇文章主要為大家介紹了不修改數(shù)組找出重復(fù)的數(shù)字Python實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
基于PyQt5制作Excel文件數(shù)據(jù)去重小工具
這篇文章主要介紹了如何利用PyQt5模塊制作一個Excel文件數(shù)據(jù)去重小工具,可以將單個或者多個Excel文件數(shù)據(jù)進行去重操作,去重的列可以通過自定義制定,需要的可以參考一下2022-04-04

