主頁(yè) > 知識(shí)庫(kù) > Qt5 實(shí)現(xiàn)主窗口狀態(tài)欄顯示時(shí)間

Qt5 實(shí)現(xiàn)主窗口狀態(tài)欄顯示時(shí)間

熱門標(biāo)簽:ai地圖標(biāo)注 地圖標(biāo)注推廣單頁(yè) 長(zhǎng)春人工智能電銷機(jī)器人官網(wǎng) n400電話申請(qǐng)多少錢 廈門crm外呼系統(tǒng)如何 如何在地圖標(biāo)注文字 女王谷地圖標(biāo)注 西藏快速地圖標(biāo)注地點(diǎn) 百應(yīng)ai電銷機(jī)器人鄭州

使用Qt Creator創(chuàng)建默認(rèn)的窗體程序后,主窗口QMainWindow有statusBar狀態(tài)欄,在此狀態(tài)欄實(shí)時(shí)顯示時(shí)間可以使用下面方法實(shí)現(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í)間更新槽函數(shù),狀態(tài)欄顯示時(shí)間
private:
  Ui::MainWindow *ui;
  QLabel *currentTimeLabel; // 先創(chuàng)建一個(gè)QLabel對(duì)象
  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的信號(hào)
  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] 獲取時(shí)間
  QDateTime current_time = QDateTime::currentDateTime();
  QString timestr = current_time.toString( "yyyy年MM月dd日 hh:mm:ss"); //設(shè)置顯示的格式
  currentTimeLabel->setText(timestr); //設(shè)置label的文本內(nèi)容為時(shí)間
}

補(bǔ)充:Qt 通過(guò)QLabel控件來(lái)顯示實(shí)時(shí)日期時(shí)間

頭文件需添加:

#include QTimer>

構(gòu)造函數(shù)中:

//日期/時(shí)間顯示
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
timer->start(1000);

定義成員函數(shù)timerUpdate()實(shí)現(xiàn)用戶界面顯示時(shí)間:

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • PyQt5 實(shí)現(xiàn)狀態(tài)欄永久顯示消息
  • python GUI庫(kù)圖形界面開發(fā)之PyQt5狀態(tài)欄控件QStatusBar詳細(xì)使用方法實(shí)例
  • python3.x+pyqt5實(shí)現(xiàn)主窗口狀態(tài)欄里(嵌入)顯示進(jìn)度條功能

標(biāo)簽:拉薩 亳州 廊坊 內(nèi)江 綿陽(yáng) 興安盟 黔東 渭南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Qt5 實(shí)現(xiàn)主窗口狀態(tài)欄顯示時(shí)間》,本文關(guān)鍵詞  Qt5,實(shí)現(xiàn),主,窗口,狀態(tài),欄,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Qt5 實(shí)現(xiàn)主窗口狀態(tài)欄顯示時(shí)間》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Qt5 實(shí)現(xiàn)主窗口狀態(tài)欄顯示時(shí)間的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章