QCombox和QDateTimeEdit组合实现今日、本月、本季、本年、自定义时间的切换
一、功能描述
实现功能:使用QCombox和QDateTimeEdit实现今日、本月、本季、本年、自定义时间的切换,今日、本月、本季、本年默认时间不可以修改,自定义时可以修改。
二、说明
说明:由于能力有限,没有找到好的方法,代码写的也比较low,也没有进行优化,只希望能给用到的人提供一种思路。
三、实现效果
效果截图如下:
四、代码示例
connect(ui->comboBox,SIGNAL(currentTextChanged(const QString)),this,SLOT(dealSelectComboxDate(const QString)));
// 函数说明:选择时间周期
void MainWindow::dealSelectComboxDate(const QString text)
{
ui->dateTimeEditBegin->setEnabled(false);
ui->dateTimeEditEnd->setEnabled(false);
if (text.contains("日"))
{
comboxSelectDayIndex();
}
else if (text.contains("月"))
{
comboxSelectMonthIndex();
}
else if (text.contains("季"))
{
comboxSelectSeasonIndex();
}
else if (text.contains("年"))
{
comboxSelectYearIndex();
}
else
{
ui->dateTimeEditBegin->setEnabled(true);
ui->dateTimeEditEnd->setEnabled(true);
}
}
// 函数说明:combox时间选择日时
void MainWindow::comboxSelectDayIndex()
{
QDate curDate = QDate::currentDate();
QString dayDate = curDate.toString("yyyy-MM-dd");
QString timeBegin = QString(" 00:00:00");
QString timeEnd = QString(" 23:59:59");
QString dateTimeBeginStr = dayDate + timeBegin;
QString dateTimeEndStr = dayDate + timeEnd;
QDateTime dateTimeBegin = QDateTime::fromString(dateTimeBeginStr, "yyyy-MM-dd hh:mm:ss");
QDateTime dateTimeEnd = QDateTime::fromString(dateTimeEndStr, "yyyy-MM-dd hh:mm:ss");
ui->dateTimeEditBegin->setDateTime(dateTimeBegin);
ui->dateTimeEditEnd->setDateTime(dateTimeEnd);
}
// 函数说明:combox时间选择月时
void MainWindow::comboxSelectMonthIndex()
{
QDate curDate = QDate::currentDate();
int days = curDate.daysInMonth();
QString yearDate = QString("%1-").arg(curDate.year());
QString monthDate = QString("%1-").arg(curDate.month());
QString monthBegin = yearDate + monthDate + QString("01") + QString(" 00:00:00");
QString monthEnd = yearDate + monthDate + QString("%1").arg(days) + QString(" 23:59:59");
QDateTime dateTimeBegin = QDateTime::fromString(monthBegin, "yyyy-MM-dd hh:mm:ss");
QDateTime dateTimeEnd = QDateTime::fromString(monthEnd, "yyyy-MM-dd hh:mm:ss");
ui->dateTimeEditBegin->setDateTime(dateTimeBegin);
ui->dateTimeEditEnd->setDateTime(dateTimeEnd);
}
// 函数说明:combox时间选择季时
void MainWindow::comboxSelectSeasonIndex()
{
QDate curDate = QDate::currentDate();
int month = curDate.month();
QString timeBegin = QString(" 00:00:00");
QString timeEnd = QString(" 23:59:59");
QString dayBeginDate = QString("01");
QString dayEndDate = QString("31");
QString monthBeginDate = QString("01-");
QString monthEndDate = QString("03-");
QString yearDate = QString("%1-").arg(curDate.year());
if (month == 1 || month == 2 || month == 3)
{
}
else if (month == 4 || month == 5 || month == 6)
{
dayEndDate = QString("30");
monthBeginDate = QString("04-");
monthEndDate = QString("06-");
}
else if (month == 7 || month == 8 || month == 9)
{
dayEndDate = QString("30");
monthBeginDate = QString("07-");
monthEndDate = QString("09-");
}
else
{
dayEndDate = QString("31");
monthBeginDate = QString("10-");
monthEndDate = QString("12-");
}
QString seasonBegin = yearDate + monthBeginDate + dayBeginDate + timeBegin;
QString seasonEnd = yearDate + monthEndDate + dayEndDate + timeEnd;
QDateTime dateTimeBegin = QDateTime::fromString(seasonBegin, "yyyy-MM-dd hh:mm:ss");
QDateTime dateTimeEnd = QDateTime::fromString(seasonEnd, "yyyy-MM-dd hh:mm:ss");
ui->dateTimeEditBegin->setDateTime(dateTimeBegin);
ui->dateTimeEditEnd->setDateTime(dateTimeEnd);
}
// 函数说明:combox时间选择年时
void MainWindow::comboxSelectYearIndex()
{
QDate curDate = QDate::currentDate();
QString yearDate = QString("%1-").arg(curDate.year());
QString yearBegin = yearDate + QString("01-") + QString("01") + QString(" 00:00:00");
QString yearEnd = yearDate + QString("12-") + QString("30") + QString(" 23:59:59");
QDateTime dateTimeBegin = QDateTime::fromString(yearBegin, "yyyy-MM-dd hh:mm:ss");
QDateTime dateTimeEnd = QDateTime::fromString(yearEnd, "yyyy-MM-dd hh:mm:ss");
ui->dateTimeEditBegin->setDateTime(dateTimeBegin);
ui->dateTimeEditEnd->setDateTime(dateTimeEnd);
}
本文为博主原创文章,未经博主允许请勿转载!作者:ISmileLi