Qt中QDateTimeEdit的具體使用
一.描述
1.QDateTimeEdit是一個(gè)用來編輯日期和時(shí)間的單行文本框,由于大繼承自QAbstractSpinBox類,可以用箭頭按鈕來調(diào)節(jié)文本內(nèi)容,也可以用鍵盤輸入。在用按鈕修改內(nèi)容時(shí)可以單獨(dú)修改某個(gè)部分(年、月、日、小時(shí)、分)
2.QDateTimeEdit可以用來單獨(dú)顯示日期(QDate),也可以單獨(dú)顯示時(shí)間(QTime),當(dāng)然也可以顯示日期時(shí)間(QDateTime)
二.QDateTime的使用
QDateTime是用來描述日期和時(shí)間的對(duì)象,他是QDate和QTime兩個(gè)類的組合,包含了年月日/小時(shí)分秒毫秒。
1、構(gòu)造函數(shù)
按照下面的方式構(gòu)造對(duì)象都是可以的。
QDate mdate = QDate(2012,1,1); QTime mtime = QTime(12,12,12); QDateTime mdate1 = QDateTime(mdate); QDateTime mdate3 = QDateTime(mdate,mtime);
2、計(jì)算時(shí)間差
int utc_time = m_datetime.offsetFromUtc(); //與標(biāo)準(zhǔn)時(shí)區(qū)的時(shí)間差 qDebug()<<"utc 時(shí)間差"<<utc_time; QDate m_date1 = QDate(2012,10,2); int day_cnt = m_date1.daysTo(QDate::currentDate()); qDebug()<<"相差天數(shù)"<<day_cnt;
3、獲取時(shí)間
QDateTime datetime = QDateTime::currentDateTime(); qDebug()<<"當(dāng)前時(shí)間"<<datetime; QDate date = QDate::currentDate(); qDebug()<<"當(dāng)前日期"<<date; QTime time = QTime::currentTime(); qDebug()<<"當(dāng)前time"<<time; int day = date.day(); qDebug()<<"當(dāng)前月下第幾日"<<day; int month = date.month(); qDebug()<<"第幾月"<<month; int year = date.year(); qDebug()<<"哪一年"<<year; int month_day_num = date.daysInMonth(); qDebug()<<"當(dāng)前月有多少天"<<month_day_num; int year_day_num = date.daysInYear(); qDebug()<<"當(dāng)前年有多少天"<<year_day_num;
三.QDateTimeEdit的應(yīng)用
1.構(gòu)造函數(shù)
m_DateTimeEdit = new QDateTimeEdit(QDateTime::currentDateTime(),this); m_DateTimeEdit = new QDateTimeEdit(QDate::currentDate(),this); m_DateTimeEdit = new QDateTimeEdit(QTime::currentTime(),this);
2.顯示格式
QDateTImeEdit里每一個(gè)部分(年月日時(shí)分秒)都是一個(gè)section,我們可以根據(jù)指定的樣式來改變顯示格式
m_DateTimeEdit->setDisplayFormat("yyyy-MM-dd-hh-mm-ss");指定的格式有時(shí)間格式符,可以直接使用
h -----------------沒有前導(dǎo)0的小時(shí)(0——12或0——23)
hh ---------------有前導(dǎo)0的小時(shí)(00——12或00——23)
H -----------------沒有前導(dǎo)0的顯示(0——23)
HH ---------------有前導(dǎo)0的顯示(00——23)
m -----------------沒有前導(dǎo)0的分鐘(0——59)
mm --------------有前導(dǎo)0的分鐘(00——59)
s ------------------沒有前導(dǎo)0的秒(0——59)
ss -----------------有前導(dǎo)0的秒(00——59)
z -------------------第二個(gè)小數(shù)部分,沒有尾隨0的毫秒(0——999)
zzz ----------------第二個(gè)小數(shù)部分,有尾隨0的毫秒(000——999)
AP/A --------------用AM/PM顯示(只用一種就可以)
ap/a ---------------用am/pm顯示
t --------------------時(shí)區(qū)
還有日期的格式符
d -------------- 沒有前導(dǎo)0的數(shù)字日期(1——31)
dd ------------ 有前導(dǎo)0的數(shù)字日期(01——31)
ddd ----------- 縮寫的本地化日期名稱(周日——周六,Sun——Sat)
dddd ---------- 完整的本地化日期名稱(星期日——星期六)
M -------------- 沒有前導(dǎo)0的數(shù)字月(1——12月)
MM ------------ 有前導(dǎo)0的數(shù)字月(01——12)
MMM --------- 縮寫的本地化月份(1月——12月)
MMMM ------- 完整的本地化月份(一月——十二月)
yy -------------- 年的后兩位
yyyy ----------- 年(4位)
效果如下:

3.日歷選擇控件
m_DateTimeEdit->setCalendarPopup(true);
效果如下:

四.信號(hào)
日期發(fā)生改變
信號(hào)中都傳遞了相對(duì)應(yīng)的QDateTime或QDate的參數(shù)。要注意的是信號(hào)發(fā)出是在數(shù)據(jù)徹底發(fā)生改變后,比如用鍵盤輸入年份,只有當(dāng)2019全輸入后才會(huì)發(fā)送信號(hào)。
QDateTimeEdit::dateTimeChanged() //日期時(shí)間發(fā)生改變 QDateTimeEdit::dateChanged() //日期發(fā)生改變 QDateTimeEdit::timeChanged() //時(shí)間發(fā)生改變
源碼:
mainwindow.c
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->resize(800,600);
QDateTime m_datetime = QDateTime::currentDateTime();
QDate mdate = QDate(2012,1,1);
QTime mtime = QTime(12,12,12);
QDateTime mdate1 = QDateTime(mdate);
QDateTime mdate3 = QDateTime(mdate,mtime);
m_DateTimeEdit = new QDateTimeEdit(m_datetime,this);
// m_DateTimeEdit = new QDateTimeEdit(QDate::currentDate(),this);
// m_DateTimeEdit = new QDateTimeEdit(QTime::currentTime(),this);
m_DateTimeEdit->setGeometry(100,100,200,40);
int utc_time = m_datetime.offsetFromUtc(); //與標(biāo)準(zhǔn)時(shí)區(qū)的時(shí)間差
qDebug()<<"utc 時(shí)間差"<<utc_time;
QDate m_date1 = QDate(2012,10,2);
int day_cnt = m_date1.daysTo(QDate::currentDate());
qDebug()<<"相差天數(shù)"<<day_cnt;
QDateTime datetime = QDateTime::currentDateTime();
qDebug()<<"當(dāng)前時(shí)間"<<datetime;
QDate date = QDate::currentDate();
qDebug()<<"當(dāng)前日期"<<date;
QTime time = QTime::currentTime();
qDebug()<<"當(dāng)前time"<<time;
int day = date.day();
qDebug()<<"當(dāng)前月下第幾日"<<day;
int month = date.month();
qDebug()<<"第幾月"<<month;
int year = date.year();
qDebug()<<"哪一年"<<year;
int month_day_num = date.daysInMonth();
qDebug()<<"當(dāng)前月有多少天"<<month_day_num;
int year_day_num = date.daysInYear();
qDebug()<<"當(dāng)前年有多少天"<<year_day_num;
m_DateTimeEdit->setDisplayFormat("yyyy-MM-dd-hh-mm-ss");
m_DateTimeEdit->setCalendarPopup(true);
}
MainWindow::~MainWindow()
{
delete ui;
}到此這篇關(guān)于Qt中QDateTimeEdit的具體使用的文章就介紹到這了,更多相關(guān)Qt QDateTimeEdit內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何用C寫一個(gè)web服務(wù)器之I/O多路復(fù)用
本文主要介紹了如何用C寫一個(gè)web服務(wù)器之I/O多路復(fù)用,本次選擇了 I/O 模型的優(yōu)化,因?yàn)樗欠?wù)器的基礎(chǔ),這個(gè)先完成的話,后面的優(yōu)化就可以選擇各個(gè)模塊來進(jìn)行,不必進(jìn)行全局化的改動(dòng)了。2021-05-05
C語言 以數(shù)據(jù)塊的形式讀寫文件詳解及實(shí)現(xiàn)代碼
本文主要介紹 C語言 以數(shù)據(jù)塊的形式讀寫文件,這里對(duì)相關(guān)知識(shí)資料做了整理,并附代碼示例,以便大家學(xué)習(xí)參考,有學(xué)習(xí)此部分知識(shí)的朋友可以參考下2016-08-08
利用Debug調(diào)試代碼解決0xC0000005:?讀取位置?0x0000000000000000?時(shí)發(fā)生訪問沖突問
這篇文章主要介紹了利用Debug調(diào)試代碼解決0xC0000005:?讀取位置?0x0000000000000000?時(shí)發(fā)生訪問沖突,本文給大家分享完美解決方案,需要的朋友可以參考下2023-03-03
C++ OpenCV實(shí)戰(zhàn)之圖像透視矯正
這篇文章主要介紹了通過C++ OpenCV實(shí)現(xiàn)圖像的透視矯正,文中的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的參考價(jià)值,感興趣的可以了解一下2022-01-01
C++實(shí)現(xiàn)移動(dòng)立方體示例講解
這篇文章主要介紹了C++實(shí)現(xiàn)移動(dòng)立方體,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12

