Qt Creator新增一個專案<Qt圖形介面應用程式>
專案名稱: DateTimeGUI
-----------------------------------------------------------------------------------------------------------
開始利用QDate和QTime來進行日期時間的操作吧!
首先mainwindow.h加入下列標頭檔
#include <QDate> #include <QTime>
#pragmaexecution_character_set("utf-8")
mainwindow.ui
加入pushbutton和label如下
利用notepad++工具開啟mainwindow.cpp, 選擇<編碼>設定為編譯成UTF-8碼
加入click事件
加入程式碼
void MainWindow::on_pushButton_clicked()
{
QDate dt(2013, 10, 30);
QString str = "今天是 ";
ui->label->setText(str + dt.toString());
}
執行測試, 按下<日期>按鈕
----------------------------------------------------------------------------------------------------------------
仿照上面複製一份pushbutton, 其text屬性值=setDate
setDate
void MainWindow::on_pushButton_2_clicked()
{
QDate dt;
dt.setDate(2013,10,30);
QString str = "今天是 ";
ui->label->setText(str + dt.toString());
}
------------------------------------------------------------------------------------------------------------------
void MainWindow::on_pushButton_3_clicked()
{
QTime tm(11, 16, 30, 55);
QString str = "hh:mm:ss.zzz: ";
ui->label->setText(str + tm.toString("hh:mm:ss.zzz"));
}
--------------------------------------------------------------------------------------------------------
setHMS
void MainWindow::on_pushButton_4_clicked()
{
QTime tm;
tm.setHMS(11, 24, 59, 333);
QString str = "hh:mm:ss.zzz: ";
ui->label->setText(str + tm.toString("hh:mm:ss.zzz"));
}
--------------------------------------------------------------------------------------------------------
抓取目前日期 & 時間
void MainWindow::on_pushButton_5_clicked()
{
QDate dt_now = QDate::currentDate();
QTime tm_now = QTime::currentTime();
ui->label->setText(dt_now.toString("yyyy/MM/dd ") + " " + tm_now.toString("hh:mm:ss.zzz"));
}
--------------------------------------------------------------------------------------------------------
日期比較
void MainWindow::on_pushButton_6_clicked()
{
QDate dt1(2013,10,30);
QDate dt2(2012,10,30);
if(dt1>dt2)
ui->label->setText(dt1.toString() + " > " + dt2.toString() );
else
ui->label->setText(dt1.toString() + " < " + dt2.toString() );
}
--------------------------------------------------------------------------------------------------------
leap year(閏年)
void MainWindow::on_pushButton_7_clicked()
{
bool isLeapYear = QDate::isLeapYear(2013);
if(isLeapYear)
ui->label->setText( "2013是閏年" );
else
ui->label->setText( "2013不是閏年" );
}
--------------------------------------------------------------------------------------------------------
Custom date formats
Custom time formats
--------------------------------------------------------------------------------------------------------
Retrieving the weekday
今天是這個禮拜的第幾天void MainWindow::on_pushButton_8_clicked()
{
QDate dt = QDate::currentDate();
int dayOfWeek = dt.dayOfWeek();
ui->label->setText( dt.toString() + " dayOfWeek = " + QString::number( dayOfWeek ) );
}
--------------------------------------------------------------------------------------------------------
Number of days
計算這個月的天數
void MainWindow::on_pushButton_9_clicked()
{
QDate dt = QDate::currentDate();
int numOfDays = dt.daysInMonth();
ui->label->setText( dt.toString() + " numOfDays = " + QString::number( numOfDays ) );
}
--------------------------------------------------------------------------------------------------------
Checking validity of a date
檢查日期
void MainWindow::on_pushButton_10_clicked()
{
QDate dt(2013,10,32);
int isValidDate = dt.isValid();
ui->label->setText( dt.toString() + " isValid = " + QString::number( isValidDate ) );
}
--------------------------------------------------------------------------------------------------------
Days to, days from
計算幾天後的日期, 計算幾天前的日期
void MainWindow::on_pushButton_11_clicked()
{
QDate dt = QDate::currentDate();
int addDates = 3; // 三天後
QDate dt_after = dt.addDays(addDates);
ui->label->setText( dt.toString() + " " + QString::number(addDates) + " 天後為 " + dt_after.toString() );
}
void MainWindow::on_pushButton_12_clicked()
{
QDate dt = QDate::currentDate();
int addDates = -3; // 三天前
QDate dt_after = dt.addDays(addDates);
ui->label->setText( dt.toString() + " " + QString::number(addDates) + " 天前為 " + dt_after.toString() );
}
參考資料: http://www.zetcode.com/gui/qt4/datetime/
範例程式(sample code): DateTimeGUI
留言列表