QDateTime 的currentDateTime()函数用于获取当前系统时间,其函数toString()函数用于
将获取的当前时间转换成字符串类型,这里,函数toString()的参数就是设置需要显示时间的格式。直接上代码......
time_widget.h
#ifndef TIMEWIDGET_H #define TIMEWIDGET_H #include <QWidget> #include <QDateTime > #include <QTimer> #include <QPalette> //----调色板类 #include <QLabel> class TimeWidget : public QWidget { Q_OBJECT public: explicit TimeWidget(QWidget *parent = 0); public slots: void slotShowTime(); private: QLabel *showTmeLabel; }; #endif //TIMEWIDGET_Htime_widget.cpp
#include "time_widget.h" TimeWidget::TimeWidget(QWidget *parent /*= 0*/) : QWidget(parent) { ////----调色背景 //QPalette p = palette(); //p.setColor(QPalette::Window, Qt::blue); //setPalette(p); //----显示时间label showTmeLabel = new QLabel(this); showTmeLabel->setGeometry(10, 5, 130, 200); //---定时器 QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(slotShowTime())); //---定时调用,这里的参数单位是 毫秒 timer->start(1000); slotShowTime(); //--初始显示时间 //resize(150, 70); //--设置显示数字大小 //----如果要设置无边创窗口,加上下面的这句代码 //setWindowFlags(Qt::FramelessWindowHint); //---设置窗口透明度 setWindowOpacity(0.8); //---窗口大小 resize(200, 200); } //------显示时间 void TimeWidget::slotShowTime() { //------获取当前时间, 保存在 time QDateTime time = QDateTime :: currentDateTime(); //----这里可以进行多种时间显示的形式 //--例如: yyyy-MM-dd hh:mm:ss QString timeStr1= time.toString("yyyy-MM-dd hh:mm:ss"); timeStr1.append("\n"); //-----例如: dd.MM.yyyy QString timeStr2 = time.toString("hh:mm:ss dd.MM.yyyy"); timeStr2.append("\n"); //--例如: ddd MMMM d yy QString timeStr3 = time.toString("ddd MMMM d yy"); timeStr3.append("\n"); //----例如: hh:mm:ss:zzz QString timeStr4 = time.toString("hh:mm:ss:zzz"); timeStr4.append("\n"); //----例如: h:m:s ap QString timeStr5 = time.toString("h:m:s ap"); //---显示时间 showTmeLabel->setText(timeStr1 + timeStr2 + timeStr3 + timeStr4 + timeStr5); }main.cpp
#include <QtWidgets/QApplication> #include "time_widget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); TimeWidget *win = new TimeWidget(); win->show(); return a.exec(); }
这里给出assistant里面关于设置QDataTime的时间格式的原文
QString QDateTime::toString(const QString & format) const Returns the datetime as a string. The format parameter determines the format of the result string. These expressions may be used for the date: Expression Output d the day as number without a leading zero (1 to 31) dd the day as number with a leading zero (01 to 31) ddd the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses the system locale to localize the name, i.e. QLocale::system(). dddd the long localized day name (e.g. 'Monday' to 'Qt::Sunday'). Uses the system locale to localize the name, i.e. QLocale::system(). M the month as number without a leading zero (1-12) MM the month as number with a leading zero (01-12) MMM the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses the system locale to localize the name, i.e. QLocale::system(). MMMM the long localized month name (e.g. 'January' to 'December'). Uses the system locale to localize the name, i.e. QLocale::system(). yy the year as two digit number (00-99) yyyy the year as four digit number These expressions may be used for the time: Expression Output h the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) hh the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) H the hour without a leading zero (0 to 23, even with AM/PM display) HH the hour with a leading zero (00 to 23, even with AM/PM display) m the minute without a leading zero (0 to 59) mm the minute with a leading zero (00 to 59) s the second without a leading zero (0 to 59) ss the second with a leading zero (00 to 59) z the milliseconds without leading zeroes (0 to 999) zzz the milliseconds with leading zeroes (000 to 999) AP or A use AM/PM display. A/AP will be replaced by either "AM" or "PM". ap or a use am/pm display. a/ap will be replaced by either "am" or "pm". t the timezone (for example "CEST") All other input characters will be ignored. Any sequence of characters that are enclosed in single quotes will be treated as text and not be used as an expression. Two consecutive single quotes ("''") are replaced by a singlequote in the output. Formats without separators (e.g. "HHmm") are currently not supported. Example format strings (assumed that the QDateTime is 21 May 2001 14:13:09): Format Result dd.MM.yyyy 21.05.2001 ddd MMMM d yy Tue May 21 01 hh:mm:ss.zzz 14:13:09.042 h:m:s ap 2:13:9 pm If the datetime is invalid, an empty string will be returned.