0
点赞
收藏
分享

微信扫一扫

Qt 获取系统时间并动态显示在状态栏statusBar

飞鸟不急 2022-02-25 阅读 163


Qt 获取系统时间并动态显示在状态栏statusBar方法说明:

头文件mainwindow.h:

#include<QDateTime>
#include<QTimer>

private slots:
void TimeUpdate();

源文件mainwindow.cpp:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);


QTimer *timer=new QTimer(this);
timer->start(1000); // 每次触发timeout信号时间间隔为1秒

connect(timer,SIGNAL(timeout()),this,SLOT(TimeUpdate()));
ShowTimeLabel = new QLabel(this);
}



void MainWindow::TimeUpdate()
{
QDateTime CurrentTime=QDateTime::currentDateTime();
QString strTime=CurrentTime.toString(" yyyy年MM月dd日 hh:mm:ss "); //设置显示的格式
ShowTimeLabel->setText(strTime);
statusBar()->addPermanentWidget(ShowTimeLabel);
statusBar()->setSizeGripEnabled(true); //设置是否显示右边的大小控制点
}

运行效果:

Qt 获取系统时间并动态显示在状态栏statusBar_#include

左下角的信息输出用showMessage实现:

QString strPosXY = QString("PosXY--(%1, %2)").arg(QString::number(x_in_Label), QString::number(y_in_Label));
//ui->statusBar->setStyleSheet("color:blue");
ui->statusBar->showMessage(strPosXY, 0);


举报

相关推荐

0 条评论