nabroyan
nabroyan

Reputation: 3275

QCalendarWidget selection color

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.

enter image description here

enter image description here

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.

Upvotes: 1

Views: 4231

Answers (2)

hank
hank

Reputation: 9853

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.

Upvotes: 3

the_naive
the_naive

Reputation: 3064

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.

Upvotes: 2

Related Questions