QT中修改QCalendarWidget样式时,令周几(表头)与日期文字颜色不同的方法,设置文字样式冲突的问题

太长不看版

Qt日历中使用setWeekdayTextFormat修改周末文字颜色和setHeaderTextFormat修改表头文字颜色发生冲突,通过找到每个周末日期来修改文字颜色。效果图和代码见文末


在Qt中用到了QCalendarWidget,根据设计给出的设计图,需对其进行修改,搜索一下有很多可以参考的方法,比如这个就比较完善了,但还需要进行一些修改

修改周末的颜色

把周末的红色去掉,改成和周一-周五一致,可调用setWeekdayTextFormat(Qt::DayOfWeek dayOfWeek, const QTextCharFormat &format)进行设置

QCalendarWidget* calendar = new QCalendarWidget(this);
QTextCharFormat weekendFormat;
weekendFormat.setForeground(QBrush(Qt::gray));
calendar->setWeekdayTextFormat(Qt::Saturday, weekendFormat);
calendar->setWeekdayTextFormat(Qt::Sunday, weekendFormat);

这样一顿操作之后获得的日历如下图所示

image.png

修改表头周x的颜色

但根据设计图,需要在保持【2】区域白底灰字的前提下,将【1】区域改为灰底白字,翻阅QCalendarWidget的函数,发现有一个setHeaderTextFormat(const QTextCharFormat &format) 但尝试之后发现会出现以下情况:
image.png
可以发现表头周末的文字颜色并没有改变,仔细看以下Qt文档可以发现

void QCalendarWidget::setHeaderTextFormat(const QTextCharFormat &format)
Sets the text char format for rendering the header to format. If you also set a weekday text format, this format’s foreground and background color will take precedence over the header’s format. The other formatting information will still be decided by the header’s format.

因为在前面对周末设置了weekday text format,所以在这里表头样式对周末不起作用。
搜索一番找到一个可能有参考意义的方案,不过这个是修改表头的背景颜色,根据这个思路去翻了一下QCalendarModelformatForCell(int, int) 的源码,发现是长这样的:

    format.setBackground(pal.brush(cg, header ? QPalette::AlternateBase : QPalette::Base));
    format.setForeground(pal.brush(cg, QPalette::Text));

佛了,为什么背景知道要区分是不是表头,前景色就不管了
到这里好像没有什么直接修改表头文字颜色的好办法了,重写QCalendarWidget类也比较麻烦,还不如把源码贴到项目里改个名。

最后的解决办法

不过办法总是有的,就是笨一点,这里用到了setDateTextFormat(QDate , QTextCharFormat) 函数,既然有冲突那就不设置特定星期x的样式,而是设置好表头样式,再一天天的设置每个属于周末的日期的文字样式,代码如下

// slot

void MyWidget::UpdateCldColor(int y, int m) {
	QTextCharFormat dateFormat;
	dateFormat.setForeground(QBrush(Qt::gray));
	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) {
			calendar->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;
			calendar->setDateTextFormat(curdate, dateFormat);
			curdate = curdate.addDays(1);
			curday = curdate.dayOfWeek();
			curmonth = curdate.month();
		}
		curdate = curdate.addDays(5);
		curday = curdate.dayOfWeek();
		curmonth = curdate.month();
	}
}
// 对calendar设置

calendar = new QCalendarWidget(this);
// 设置表头Mon - Fri颜色
QTextCharFormat fmt;
fmt.setForeground(QBrush(Qt::white));
fmt.setBackground(QBrush(Qt::gray));
calendar->setHeaderTextFormat(fmt);
//设置表头Sat - Sun颜色
QTextCharFormat weekendFormat;
weekendFormat.setForeground(QBrush(Qt::white));
calendar->setWeekdayTextFormat(Qt::Saturday, weekendFormat);
calendar->setWeekdayTextFormat(Qt::Sunday, weekendFormat);
// 出现时先调用一次
UpdateCldColor(calendar->monthShown(), calendar->yearShown());
// 每次翻页重新绘制
connect(calendar, SIGNAL(currentPageChanged(int, int)), this, SLOT(UpdateCldColor(int, int)));

最终效果如下
image.png
完成!如果有其他更好的办法欢迎指出~

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值