时间,不外乎就是获取时间,必要时转化一下时间显示格式.直接看例子吧
QDateEdit 和 QTimeEdit
QDateEdit
- date:保存了部件的显示日期。
- minimumDate:定义了用户可以设置的最小日期。
- maximumDate:定义了用户可以设置的最大日期。
- displayFormat:包含了一个字符串用于格式化日期。
QTimeEdit
- time:保存了部件的显示时间。
- minimumTime:定义了用户可以设- 置的最小时间。
- maximumTime:定义了用户可以设置的最大时间。
- displayFormat:包含了一个字符串用于格式化时间。
QDateTime
时间调整和范围设置
| 函数 |
描述 |
| QDateTime addDays(qint64 ndays) const |
|
| QDateTime addMSecs(qint64 msecs) const |
|
| QDateTime addMonths(int nmonths) const |
|
| QDateTime addSecs(qint64 s) const |
|
| QDateTime addYears(int nyears) const |
|
| void setDate(const QDate &date) |
|
| void setTime(const QTime &time) |
|
| void setTimeSpec(Qt::TimeSpec spec) |
区域时间 |
| void setTimeZone(const QTimeZone &toZone) |
时区 |
判断
| 函数 |
描述 |
| bool isDaylightTime() const |
|
| bool isNull() const |
|
| bool isValid() const |
|
格式转化
| 函数 |
描述 |
| CFDateRef toCFDate() const |
|
| QDateTime toLocalTime() const |
|
| qint64 toMSecsSinceEpoch() const |
|
| NSDate * toNSDate() const |
|
| QDateTime toOffsetFromUtc(int offsetSeconds) const |
|
| qint64 toSecsSinceEpoch() const |
|
| QString toString(const QString &format) const |
|
| QString toString(Qt::DateFormat format = Qt::TextDate) const |
|
| QString toString(QStringView format) const |
|
| QDateTime toTimeSpec(Qt::TimeSpec spec) const |
|
| QDateTime toTimeZone(const QTimeZone &timeZone) const |
|
| QDateTime toUTC() const |
|
格式控制
| 字符 |
表示 |
| 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 whole second without a leading zero (0 to 59) |
| ss |
the whole second with a leading zero where applicable (00 to 59) |
| z |
the fractional part of the second, to go after a decimal point, without trailing zeroes (0 to 999). Thus “s.z” reports the seconds to full |
| zzz |
the fractional part of the second, to millisecond precision, including trailing zeroes where applicable (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”) |
QDateTimeEdit
时间日期函数,可以设置显示格式,时间范围,调用日历
set
| 函数 |
描述 |
| oid setCalendarPopup(bool enable) |
弹出日历选择 |
| void setCalendarWidget(QCalendarWidget *calendarWidget) |
弹出的日历窗体 |
| void setCurrentSection(QDateTimeEdit::Section section) |
设置现在的session |
| void setCurrentSectionIndex(int index) |
|
| void setDateRange(const QDate &min, const QDate &max) |
data 范围 |
| void setDateTimeRange(const QDateTime &min, const QDateTime &max) |
datetime 范围 |
| void setDisplayFormat(const QString &format) |
显示格式 |
| void setMaximumDate(const QDate &max) |
|
| void setMaximumDateTime(const QDateTime &dt) |
|
| void setMaximumTime(const QTime &max) |
|
| void setMinimumDate(const QDate &min) |
|
| void setMinimumDateTime(const QDateTime &dt) |
|
| void setMinimumTime(const QTime &min) |
|
| void setSelectedSection(QDateTimeEdit::Section section) |
设置当前的seeion |
| void setTimeRange(const QTime &min, const QTime &max) |
|
| void setTimeSpec(Qt::TimeSpec spec) |
区域 |
slots
void setDate(const QDate &date)
void setDateTime(const QDateTime &dateTime)
void setTime(const QTime &time)
signal
void dateChanged(const QDate &date)
void dateTimeChanged(const QDateTime &datetime)
void timeChanged(const QTime &time)
mywidget::mywidget()
{
setWindowTitle("my widget test");
QVBoxLayout *layout=new QVBoxLayout(this);
QDateTimeEdit *pdate_time_edit=new QDateTimeEdit(QDateTime::currentDateTime(),this);
pdate_time_edit->setDateRange(QDate::currentDate().addDays(-365),QDate::currentDate().addDays(365));
pdate_time_edit->setTimeRange(QTime::currentTime().addMSecs(-10),QTime::currentTime().addMSecs(10));
pdate_time_edit->setDisplayFormat("yyyy-MM-dd HH:mm:ss");
pdate_time_edit->setCalendarPopup(true);
layout->addWidget(pdate_time_edit);
qDebug()<<"Date:"<<pdate_time_edit->date();
qDebug()<<"date_time:"<<pdate_time_edit->dateTime();
connect(pdate_time_edit,QOverload<const QDate&>::of(&QDateTimeEdit::dateChanged),[&pdate_time_edit](QDate date){
qDebug()<<"Date have change,now date is "<<date;
});
// 各部分对应的值
QString strYear = pdate_time_edit->sectionText(QDateTimeEdit::YearSection);
QString strMonth = pdate_time_edit->sectionText(QDateTimeEdit::MonthSection);
QString strDay = pdate_time_edit->sectionText(QDateTimeEdit::DaySection);
QString strHour = pdate_time_edit->sectionText(QDateTimeEdit::HourSection);
QString strMinute = pdate_time_edit->sectionText(QDateTimeEdit::MinuteSection);
QString strSecond = pdate_time_edit->sectionText(QDateTimeEdit::SecondSection);
qDebug()<<strYear<<"年"<<strMonth<<"月"<<strDay<<"日\n"<<strHour<<":"<<strMinute<<":"<<strSecond;
this->show();
}