效果图
先放一个效果图以供大家参考,大家可以根据自己需要的效果来调整自己的控件,日历控件实现了自定义日历选择框,设置了表头颜色,设置日历当天重要事件提醒功能。
设置表头样式
setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);//去掉水平表头setLocale(QLocale(QLocale::Chinese));setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames); //单字母setFirstDayOfWeek(Qt::Monday);QTextCharFormat format;format.setForeground(QColor(0, 211, 255));
// format.setForeground(QColor(255, 255, 255));format.setBackground(QColor(7, 34, 64));setHeaderTextFormat(format);setWeekdayTextFormat(Qt::Saturday, format);setWeekdayTextFormat(Qt::Sunday, format);UpdateCldColor(yearShown(),monthShown());
设置周六周日表头颜色
void MyCalendar::UpdateCldColor(int y, int m)
{QTextCharFormat dateFormat;dateFormat.setForeground(QBrush(Qt::white));int d = 1;QDate curdate = QDate(y, m, d);int curday = curdate.dayOfWeek();int curmonth = curdate.month();// 找到第一个星期六while (curdate.isValid() && curday != 6){if (curday == 7){setDateTextFormat(curdate, dateFormat);}curdate = curdate.addDays(1);curday = curdate.dayOfWeek();}// 给每个周末设颜色while (curdate.isValid() && curmonth == m){for (int i = 0; i < 2; i++){if (curmonth != m) break;setDateTextFormat(curdate, dateFormat);curdate = curdate.addDays(1);curday = curdate.dayOfWeek();curmonth = curdate.month();}curdate = curdate.addDays(5);curday = curdate.dayOfWeek();curmonth = curdate.month();}}
设置状态
void MyCalendar::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{QCalendarWidget::paintCell(painter,rect,date);painter->save();QPixmap num_map;num_map.load(":/images/calendar_num.png");
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))QRect temp_rect;temp_rect = rect;temp_rect.setWidth(temp_rect.width()-8 );temp_rect.setHeight(temp_rect.height()-10);
#elseQRect temp_rect;QMargins mar(4,5,4,5);temp_rect = rect.marginsAdded(mar);
#endifnum_map.scaled(temp_rect.size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);painter->drawPixmap(temp_rect,num_map);painter->restore();if(date == selectedDate()){painter->save();#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))QRect temp_rect;temp_rect = rect;temp_rect.setWidth(temp_rect.width()-8 );temp_rect.setHeight(temp_rect.height()-10);
#elseQRect temp_rect;QMargins mar(-4,-5,-4,-5);temp_rect = rect.marginsAdded(mar);
#endifQPixmap temp = m_backgroundPixMap.scaled(temp_rect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); // 缩放图片以适应单元格大小painter->drawPixmap(temp_rect,temp);painter->restore();}#if 1QBrush brush;brush.setStyle(Qt::SolidPattern);brush.setColor(Qt::red);int d = 1;QDate startTime = QDate(yearShown(), monthShown(),d);QDate endTime = startTime.addMonths(1).addDays(-1);if(startTime <= date && date <= endTime ){painter->save();QString datestr;datestr= date.toString("yyyyMMdd");QString temp_file;foreach (QFileInfo fileinfo, m_infolist){temp_file = fileinfo.absoluteFilePath();if(temp_file.contains(datestr)){brush.setColor(Qt::green);}else{}// 绘制标记painter->setBrush(brush); // 设置标记的颜色QPoint point(rect.topRight().x() - 15, rect.topRight().y() + 13);painter->drawEllipse(point, 3, 3); // 绘制一个小圆点作为标记}
#if 0for(int i=0;i<m_contents.size();i++){QString file_name = m_contents[i].file_name;if(file_name.contains(datestr)){brush.setColor(Qt::green);}else{}// 绘制标记painter->setBrush(brush); // 设置标记的颜色QPoint point(rect.topRight().x() - 15, rect.topRight().y() + 13);painter->drawEllipse(point, 3, 3); // 绘制一个小圆点作为标记}
#endifpainter->restore();}
#endif}
再来段QSS样式美化
QWidget
{background-color:#00274F;color:#F0F0F0;
}/**********************************QCalendarWidget*****************************/
QCalendarWidget {font-size: 16px;min-width: 340px;min-height: 260px;color: white;}QCalendarWidget QAbstractItemView#qt_calendar_calendarview
{font-size: 16px;color: white;border: 1px solid #4b8eb5;background-color: rgb(7,34,64);selection-background-color:transparent;selection-color: white;/* alternate-background-color:rgb(50,50,50); */ }QCalendarWidget QAbstractItemView#qt_calendar_calendarview:disabled
{color: rgb(67, 95, 106);border-color: #999999;background-color: #666666;
}QCalendarWidget QWidget {alternate-background-color: rgb(7, 36, 70);border-image:none;
}QCalendarWidget QToolButton#qt_calendar_prevmonth {width: 26px;height: 26px;qproperty-iconSize: 32px;border-image:none;}QCalendarWidget QToolButton#qt_calendar_nextmonth {width: 26px;height: 26px;qproperty-iconSize: 32px;border-image:none;}
QCalendarWidget QToolButton:hover {background-color: rgb(7,37,72); /* 悬浮时的背景色 */color: rgb(0, 255, 255); /* 悬浮时的文字颜色 *//* 其他悬浮时的样式设置 */
}
QCalendarWidget QToolButton:precced {background-color: rgb(7,37,72); /* 悬浮时的背景色 */color: rgb(0, 255, 255); /* 悬浮时的文字颜色 *//* 其他悬浮时的样式设置 */
}