一、调用
通过QDateTime中的setCalendarPopup(true)方法调用。
ui->dateEdit->setCalendarPopup(true);
二、基础设置
1.设置默认显示时间
ui->dateEdit->setDateTime(QDateTime::currentDateTime());
2.设置时间范围:最近三年
ui->dateEdit->setMinimumDate(QDate::currentDate().addDays(- 365 * 3));
ui->dateEdit->setMaximumDate(QDate::currentDate());
3.导航栏不可见
QCalendarWidget *calendar = ui->dateEdit->calendarWidget();
calendar->setNavigationBarVisible(false);
4.网格线
calendar->setGridVisible(true);
5.周几栏竖向排列位置
calendar->setFirstDayOfWeek(Qt::Saturday);
6.周几栏文字类型:默认是 QCalendarWidget::ShortDayNames
calendar->setHorizontalHeaderFormat(QCalendarWidget::NoHorizontalHeader);
三、QSS样式设置
1.QDateEdit样式
(1)未设置QSS、且未调用日历的默认样式:
(2)调用日历setCalendarPopup(true)后的默认样式:
(3)样式设置:正常状态与选中状态
/*正常状态*/
QDateEdit#dateEdit_timeEnd{
font:20px Microsoft YaHei;
color:rgb(32, 32, 32);
background-color:rgb(255, 204, 229, 80);
border-color:rgb(255, 153, 204);
border-style:solid;
border-radius:10px;
border-width:5px;
}
/*选中状态*/
QDateEdit#dateEdit:focus{
font:20px Microsoft YaHei;
border-color:rgb(255, 102, 178); /*边框颜色*/
border-style:solid; /*边框内容*/
border-radius:10px; /*边框圆角弧度*/
border-width:5px;
}
(4)日历下拉图标设置
QDateEdit#dateEdit:drop-down{
border-image:url(:/Picture/calendar-icon.png);
width:40px;
}
2.QDateEdit完整代码:
QDateEdit#dateEdit{
font:20px Microsoft YaHei;
color:rgb(32, 32, 32);
background-color:rgb(255, 204, 229, 80);
border-color:rgb(255, 153, 204);
border-style:solid;
border-radius:10px;
border-width:5px;
}
QDateEdit#dateEdit:focus{
font:20px Microsoft YaHei;
border-color:rgb(255, 102, 178); /*边框颜色*/
border-style:solid; /*边框内容*/
border-radius:10px; /*边框圆角弧度*/
border-width:5px;
}
QDateEdit#dateEdit:drop-down{
border-image:url(:/Picture/calendar-icon.png);
width:40px;
}
二.QCalendarWidget日历样式设置
1.基础说明
QCalendarWidget 是一个复杂的 widget,由多个个 QToolButton, QSpinBox, QMenu, QTableView 等组成。采用的方法是打印组成 QCalendarWidget 的 widget 的 className 和 objectName,然后 QSS 每个 widget。
QCalendarWidget *calendar = ui->dateEdit->calendarWidget();
dumpStructure(calendar, 0);
void CalendarWidget::dumpStructure(const QObject *obj, int spaceCount)
{
qDebug() << QString("%1 %2 : %3")
.arg("", spaceCount)
.arg(obj->metaObject()->className())
.arg(obj->objectName());
QObjectList list = obj->children();
foreach(QObject *child, list){
dumpStructure(child, spaceCount + 4);
}
}
打印结果:
" QCalendarWidget : "
" QVBoxLayout : "
" QCalendarModel : "
" QCalendarView : qt_calendar_calendarview"
" QWidget : qt_scrollarea_viewport"
" QWidget : qt_scrollarea_hcontainer"
" QScrollBar : "
" QBoxLayout : "
" QWidget : qt_scrollarea_vcontainer"
" QScrollBar : "
" QBoxLayout : "
" QStyledItemDelegate : "
" QHeaderView : "
" QWidget : qt_scrollarea_viewport"
" QWidget : qt_scrollarea_hcontainer"
" QScrollBar : "
" QBoxLayout : "
" QWidget : qt_scrollarea_vcontainer"
" QScrollBar : "
" QBoxLayout : "
" QItemSelectionModel : "
" QHeaderView : "
" QWidget : qt_scrollarea_viewport"
" QWidget : qt_scrollarea_hcontainer"
" QScrollBar : "
" QBoxLayout : "
" QWidget : qt_scrollarea_vcontainer"
" QScrollBar : "
" QBoxLayout : "
" QItemSelectionModel : "
" QTableCornerButton : "
" QItemSelectionModel : "
" QWidget : qt_calendar_navigationbar"
" QPrevNextCalButton : qt_calendar_prevmonth"
" QPrevNextCalButton : qt_calendar_nextmonth"
" QToolButton : qt_calendar_monthbutton"
" QMenu : "
" QAction : "
" QAction : "
" QAction : "
" QAction : "
" QAction : "
" QAction : "
" QAction : "
" QAction : "
" QAction : "
" QAction : "
" QAction : "
" QAction : "
" QAction : "
" QToolButton : qt_calendar_yearbutton"
" QSpinBox : qt_calendar_yearedit"
" QLineEdit : qt_spinbox_lineedit"
" QWidgetLineControl : "
" QValidator : qt_spinboxvalidator"
" QHBoxLayout : "
" QCalendarDelegate : "
" QCalendarTextNavigator : "
2.QCalendarWidget样式
分成三个部分来分别说明不同部位QSS的样式设置。QCalendarWidget设置QSS的时候用全局定义,不要带空间名称。
(1)设置总的背景栏颜色、上一个月图标、下一个月图标、月份选择、年份选择样式。默认样式:
标题背景:
#qt_calendar_navigationbar{
font:18px Microsoft YaHei;
background-color:rgb(196, 196, 249);
}
如果需要将年月选择栏背景颜色和日期背景颜色设置为同一个颜色,可以选择设置QWidget。
QCalendarWidget QWidget{
background-color:rgb(255, 255, 153);
}
上一个月、下一个月跳转图标。 如果设置了QDateEdit的时间范围(本例:当前时间),下一个月按钮样式自动回变成灰色的不可点击状态(上一个月图标同理),不需要单独去设置上一个月、下一个月的不可点击状态样式。
#qt_calendar_prevmonth{
qproperty-icon:url(:/Picture/iconGate_blue.png);
}
#qt_calendar_nextmonth{
qproperty-icon:url(:/Picture/iconGate_red.png);
}
月份选择分为两个部分,当前月的正常状态与选择状态,月份下拉框。
QToolButton#qt_calendar_monthbutton{
color:rgb(255, 51, 153);
background-color:rgb(244, 244, 244);
}
QCalendarWidget QMenu{
font:18px Microsoft YaHei;
left:5px;
color:rgb(0, 128, 255);
background-color:rgb(196, 196, 249);
}
年份选择修改样式其实就是QSpinBox修改样式。
/*年份选择:正常状态*/
QToolButton#qt_calendar_yearbutton{
color:rgb(255, 51, 153);
background-color:rgb(244, 244, 244);
}
/*年份选择:修改时*/
QSpinBox#qt_calendar_yearedit{
font:18px Microsoft YaHei;
color:rgb(255, 178, 102);
background:rgb(255, 209, 204); /*文本框背景颜色*/
selection-color:rgb(255, 255, 255); /*选中字体颜色*/
selection-background-color:rgb(204, 153, 255); /*选中背景颜色*/
}
/*年份选择:向上箭头*/
QSpinBox#qt_calendar_yearedit::up-arrow{
image:url(:/Picture/44.png);
}
/*年份选择:向下箭头*/
QSpinBox#qt_calendar_yearedit::down-arrow{
image:url(:/Picture/44.png);
}
/*年份选择:向上图标*/
QSpinBox#qt_calendar_yearedit::up-button{
/*border-image:url(:/Picture/top.png);*/
color:rgb(255, 255, 153);
}
/*年份选择:向下图标*/
QSpinBox#qt_calendar_yearedit::down-button{
/*border-image:url(:/Picture/bottom.png);*/
color:rgb(255, 255, 153);
}
(2)周一到周末栏:修改字体颜色、背景色。
修改背景色。周一到周五字体的颜色和日期当中周一到周五的颜色是同步的设置的,周六和周末的字体颜色和日期当中的周六周末的颜色是同步设置的。如果在此处修改字体颜色,后续就不要设置日期当中的字体颜色了,否则,日期当中的周一到周五的颜色设置会覆盖此处的字体颜色设置。
QCalendarWidget QTableView
{
alternate-background-color:rgb(204, 229, 255);
color:rgb(0, 128, 255); /*字体颜色*/
}
周六周末的颜色是单独在代码里面设置的,不在QSS里。
QCalendarWidget *calendar = ui->dateEdit_timeEnd->calendarWidget();
QTextCharFormat weekendFormat;
weekendFormat.setForeground(QBrush(QColor(153, 51, 255)));
calendar->setWeekdayTextFormat(Qt::Saturday, weekendFormat);
calendar->setWeekdayTextFormat(Qt::Sunday, weekendFormat);
(3)日期栏:背景色、当月字体颜色、非当月颜色、当前选中日期颜色、当前选中日期背景色。
背景色。
#qt_calendar_calendarview{
background-color:rgb(229, 229, 252);
}
/*选中月日期*/
QCalendarWidget QAbstractItemView:enabled{
font:18px Microsoft YaHei;
color:rgb(0, 204, 204);
selection-color:rgb(255, 255, 255); /* 当前选中日期:字体颜色*/
selection-background-color:rgb(0, 102, 102); /* 当前选中日期:背景颜色*/
}
/*非选中月日期*/
QCalendarWidget QAbstractItemView:disabled{
color:rgb(204, 255, 204);
}
三、所有QSS代码
/*正常状态*/
QDateEdit#dateEdit_timeEnd{
font:20px Microsoft YaHei;
color:rgb(32, 32, 32);
background-color:rgb(255, 204, 229, 80);
border-color:rgb(255, 153, 204);
border-style:solid;
border-radius:10px;
border-width:5px;
}
/*选中状态*/
QDateEdit#dateEdit_timeEnd:focus{
font:20px Microsoft YaHei;
border-color:rgb(255, 102, 178); /*边框颜色*/
border-style:solid; /*边框内容*/
border-radius:10px; /*边框圆角弧度*/
border-width:5px;
}
/*日历icon*/
QDateEdit#dateEdit_timeEnd:drop-down{
border-image:url(:/Picture/calendar-icon.png);
width:40px;
}
/*日历主体背景颜色*/
#qt_calendar_calendarview{
background-color:rgb(229, 229, 252);
}
/*日历标题栏*/
#qt_calendar_navigationbar{
font:18px Microsoft YaHei;
background-color:rgb(196, 196, 249);
}
QCalendarWidget QWidget{
background-color:rgb(255, 255, 153); /*年月选择栏背景颜色和日期背景颜色设置为同一个颜色*/
}
/*箭头:上一个月*/
#qt_calendar_prevmonth{
qproperty-icon:url(:/Picture/iconGate_blue.png);
}
/*箭头:下一个月*/
#qt_calendar_nextmonth{
qproperty-icon:url(:/Picture/iconGate_red.png);
}
/*月份选择:正常状态*/
QToolButton#qt_calendar_monthbutton{
color:rgb(255, 51, 153);
background-color:rgb(244, 244, 244);
}
/*月份选择:下拉框*/
QCalendarWidget QMenu{
font:18px Microsoft YaHei;
left:5px;
color:rgb(0, 128, 255);
background-color:rgb(196, 196, 249);
}
/*年份选择:正常状态*/
QToolButton#qt_calendar_yearbutton{
color:rgb(255, 51, 153);
background-color:rgb(244, 244, 244);
}
/*年份选择:修改时*/
QSpinBox#qt_calendar_yearedit{
font:18px Microsoft YaHei;
color:rgb(255, 178, 102);
background:rgb(255, 209, 204); /*文本框背景颜色*/
selection-color:rgb(255, 255, 255); /*选中字体颜色*/
selection-background-color:rgb(204, 153, 255); /*选中背景颜色*/
}
/*年份选择:向上箭头*/
QSpinBox#qt_calendar_yearedit::up-arrow{
image:url(:/Picture/44.png);
}
/*年份选择:向下箭头*/
QSpinBox#qt_calendar_yearedit::down-arrow{
image:url(:/Picture/44.png);
}
/*年份选择:向上图标*/
QSpinBox#qt_calendar_yearedit::up-button{
/*border-image:url(:/Picture/top.png);*/
color:rgb(255, 255, 153);
}
/*年份选择:向下图标*/
QSpinBox#qt_calendar_yearedit::down-button{
/*border-image:url(:/Picture/bottom.png);*/
color:rgb(255, 255, 153);
}
/*周一 ~ 周末*/
QCalendarWidget QTableView
{
alternate-background-color:rgb(204, 229, 255);
color:rgb(0, 128, 255); /*字体颜色*/
}
/*选中月日期*/
QCalendarWidget QAbstractItemView:enabled{
font:18px Microsoft YaHei;
color:rgb(0, 204, 204);
selection-color:rgb(255, 255, 255); /* 当前选中日期:字体颜色*/
selection-background-color:rgb(0, 102, 102); /* 当前选中日期:背景颜色*/
}
/*非选中月日期*/
QCalendarWidget QAbstractItemView:disabled{
color:rgb(204, 255, 204);
}
(如有错误,敬请批评指正。)