美文网首页
Qt之QDateTimeEdit

Qt之QDateTimeEdit

作者: YBshone | 来源:发表于2017-05-16 14:47 被阅读0次

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
部件基本使用
部件基本使用.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);
设置日期时间格式
设置日期时间.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天。
效果


日期时间范围

源码

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)即可。
效果


日历

源码

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

    QDateTime类提供了一个部件,用于编辑日期和时间。QDateTimeEdit允许用户编辑日期,通过使用键盘或...

  • [转]Qt学习之样式表

    Qt学习之样式表

  • Qt设置程序图标

    说明 本文参考QT 设置程序图标、QT5.8.0 vs2013平台更改程序运行icon图标和# Qt之任务栏系统托...

  • Qt QThread类

    参考博客1:QThread详解参考博客2:Qt之线程(QThread)参考博客3:QT 多线程程序设计参考博客4:...

  • Fedora19 如何安装QT

    sudo yum install qt qt-devel qt-x11 qt-doc qt-demos qt-ex...

  • QT 反射机制的简单使用

    Qt反射前期准备 [以下内容来自博客# Qt5之反射机制(内省),转载请注意说明出处] 首先得继承于Q_Objec...

  • Qt Designer使用技巧

    编译Qt Designer 安装Qt时,选择安装源码 用Qt Creator打开C:\Qt\Qt5.5.0\5.5...

  • QCryptographicHash

    参考Qt之QCryptographicHash QCryptographicHash类--提供了生成密码散列的方法...

  • Qt 安装环境

    Qt 安装 1.1 QT下载地址 http://download.qt.io/archive/qt/ 1.1.1 ...

  • QT5.9+MSVC2015+OPENCV4.0.0搭建

    1.资源准备 QT Qt所有版本http://download.qt.io/archive/qt/我下载了QT5....

网友评论

      本文标题:Qt之QDateTimeEdit

      本文链接:https://www.haomeiwen.com/subject/lvslxxtx.html