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); /* 悬浮时的文字颜色 */
    /* 其他悬浮时的样式设置 */
}
相关推荐
lihao lihao1 小时前
C++ set和map
开发语言·c++·算法
小陈phd1 小时前
langGraph从入门到精通(三)——基于LangGraph的智能问答系统开发:Python单代理架构实战
开发语言·python·架构
电子_咸鱼2 小时前
Linux IPC 实战:管道与共享内存的使用场景 + 底层原理全剖析
linux·运维·服务器·开发语言·网络·vscode·qt
smile_5me2 小时前
RK3588 csm400b调试记录
c语言·开发语言
C_心欲无痕2 小时前
JavaScript 常见算法与手写函数实现
开发语言·javascript·算法
客卿1232 小时前
C语言实现数组串联--力扣冒险
c语言·开发语言·leetcode
yaso_zhang2 小时前
openssl版本匹配问题
qt
客卿1232 小时前
1/14-C语言重排数组
c语言·开发语言·算法
不穿格子的程序员2 小时前
从零开始刷算法——二叉树篇:验证二叉搜索树 + 二叉树中第k小的元素
java·开发语言·算法
郝学胜-神的一滴2 小时前
Python方法类型详解:类方法、静态方法与实例方法
开发语言·python·程序人生