QCalendarWidget selection color

左心房为你撑大大i 提交于 2020-01-03 03:33:17

问题


I have a QCalendarWidget and some days of month are colored (for example holidays are red). When I select a day which is colored, selection clears the color and I can't see it's original color. But when I deselect that day - color is back. Please see in pictures.

Is there a way to keep color even if a day is selected? I know that there is a way to do this for QTableView with delegates, but I can't find anything like this for QCalendarWidget. Any Ideas? Thank you for your time.


回答1:


You can get access to the internal QTableView object of your calendar widget like this:

QCalendarWidget *c = new QCalendarWidget;

QTableView *view = c->findChild<QTableView*>("qt_calendar_calendarview");
if (view)
{
    view->setItemDelegate(new MySuperCalendarDelegate);
}

Then you can use a custom delegate that will set proper background and foreground colors.

Also you can check my previous answer on QCalendarWidget styling.




回答2:


I know it's more than a year, if I understood the question correctly, I think I found a better solution to this. In my case, every time a date is selected, I set the date to yellow color doing the following:

QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);
m_ui->calender->setDateTextFormat(date, fmt);

and that very time I also set the stylesheet of the QCalenderWidget like this:

setStyleSheet("QTableView{selection-background-color: yellow}")

If I need to de-select, I set the date color to the original, which is white and also do the following:

setStyleSheet("QTableView{selection-background-color: yellow}")

This works great for me.



来源:https://stackoverflow.com/questions/29643063/qcalendarwidget-selection-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!