Qt之QDateTimeEdit

QDateTime类提供了一个部件,用于编辑日期和时间。
QDateTimeEdit允许用户编辑日期,通过使用键盘或箭头键来增加和减少日期和时间值。箭头键可以在QDateTimeEdit内进行部分移动,日期和时间的格式按照setDisplayFormat()设置的显示

前言
QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
QString str = time.toString("yyyy-MM-dd hh:mm:ss ddd"); //设置显示格式
label->setText(str);//在标签上显示时间

*有无指针 .函数 与 ->函数的区别

QString QDateTime::toString ( const QString& format ) const

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'). .

dddd  the long localized day name (e.g. 'Monday' to '[Qt::Sunday]

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 

MMMM  the long localized month name (e.g. 'January' to 'December'). 

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)

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  use AM/PM display. *AP* will be replaced by either "AM" or "PM".

ap  use am/pm display. *ap* will be replaced by either "am" or "pm".

All other input characters will be ignored. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Two consecutive singlequotes ("''") are replaced by a singlequote in the output.
Example format strings (assumed that the [QDateTime](http://hi.baidu.com/fc/editor/qdatetime.html)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
部件基本使用
Qt之QDateTimeEdit_第1张图片
部件基本使用.gif

默认情况下,如果QDateTimeEdit构造时不指定日期时间,系统会为其设置一个和本地相同的日期时间格式(右下角可更改本地日期时间格式),并且值为:2000年1月1日 0时0分0秒。

QDateTimeEdit *dateTimeEdit = new QDateTimeEdit(this);
QDateTimeEdit *dateTimeEdit2 = new QDateTimeEdit(QDateTime::currentDateTime(), this);
QDateTimeEdit *dateEdit = new QDateTimeEdit(QDate::currentDate(), this);
QDateTimeEdit *timeEdit = new QDateTimeEdit(QTime::currentTime(), this);
设置日期时间格式
Qt之QDateTimeEdit_第2张图片
设置日期时间.png

源码

QDateTimeEdit *dateTimeEdit = new QDateTimeEdit(this);
QDateTimeEdit *dateTimeEdit2 = new QDateTimeEdit(QDateTime::currentDateTime(), this);
QDateTimeEdit *dateEdit = new QDateTimeEdit(QDate::currentDate(), this);
QDateTimeEdit *timeEdit = new QDateTimeEdit(QTime::currentTime(), this);

// 设置日期时间格式
dateTimeEdit->setDisplayFormat("yyyy-MM-dd HH:mm:ss");
dateTimeEdit2->setDisplayFormat("yyyy/MM/dd HH-mm-ss");
dateEdit->setDisplayFormat("yyyy.M.d");
timeEdit->setDisplayFormat("H:mm");

yyyy:年,4个数表示。
MM:月,01-12。
dd:日,01-31。
HH:时,00-23。
mm:分,00 - 59。
ss:秒,00-59。

日期时间范围

创建了QDateTimeEdit对象,并设置其日期时间为今天(currentDate),同时限制有效日期的范围:距离今天±365天。
效果


Qt之QDateTimeEdit_第3张图片
日期时间范围

源码

QDateTimeEdit *dateEdit = new QDateTimeEdit(QDate::currentDate(), this);
dateEdit->setMinimumDate(QDate::currentDate().addDays(-365)); // -365天
dateEdit->setMaximumDate(QDate::currentDate().addDays(365)); // +365天

其他同功能的有用函数:setDateTimeRange()、setDateRange()、setTimeRange()、setMaximumDateTime()和setMinimumDateTime()、setMinimumTime()和setMaximumTime()。

显示日历

默认情况下只能通过鼠标点击上下箭头来改变日期时间,如果要弹出日期控件,只需调用setCalendarPopup(true)即可。
效果


Qt之QDateTimeEdit_第4张图片
日历

源码

QDateTimeEdit *dateEdit = new QDateTimeEdit(QDate::currentDate(), this);
dateEdit->setMinimumDate(QDate::currentDate().addDays(-365)); // -365天
dateEdit->setMaximumDate(QDate::currentDate().addDays(365)); // +365天
dateEdit->setCalendarPopup(true); // 日历弹出

这时,上/下箭头就变为下拉箭头了。当点击下拉箭头之后,就会弹出日历控件,由于我们设置了日期范围,所以不在范围内的日期是无法选择的(disabled)。

你可能感兴趣的:(Qt之QDateTimeEdit)