QT日历控件重写美化

效果图

先放一个效果图以供大家参考,大家可以根据自己需要的效果来调整自己的控件,日历控件实现了自定义日历选择框,设置了表头颜色,设置日历当天重要事件提醒功能。

设置表头样式

cpp 复制代码
    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());

设置周六周日表头颜色

cpp 复制代码
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();
    }

}

设置状态

cpp 复制代码
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);
#else
    QRect temp_rect;
    QMargins mar(4,5,4,5);
    temp_rect = rect.marginsAdded(mar);
#endif

    num_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);
#else
    QRect temp_rect;
    QMargins mar(-4,-5,-4,-5);
    temp_rect = rect.marginsAdded(mar);
#endif
        QPixmap temp = m_backgroundPixMap.scaled(temp_rect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); // 缩放图片以适应单元格大小
        painter->drawPixmap(temp_rect,temp);
        painter->restore();
    }


#if 1
    QBrush 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 0
        for(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); // 绘制一个小圆点作为标记

        }
#endif
        painter->restore();
    }
#endif


}

再来段QSS样式美化

cpp 复制代码
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); /* 悬浮时的文字颜色 */
    /* 其他悬浮时的样式设置 */
}
相关推荐
用户805533698034 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner4 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz9 天前
QML Hello World 入门示例
qt
xcyxiner12 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner13 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner13 天前
DicomViewer (添加模型类)3
qt
xcyxiner14 天前
DicomViewer (目录调整) 2
qt
xcyxiner14 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00616 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术16 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript