Tiffany
Tiffany

Reputation: 45

Change Horizontal Header Background Color of QCalendarWidget

I am currently using Eclipse 3.5.2 and Qt Jambi 4.7.2 in Ubuntu 11.04 Natty Narwhal

I have a class that currently extends QCalendarWidget. I am trying to use style sheets to style my calendar widget. Right now, I am using QAbstractItemView to color the background but it only changes the background color of the cells with dates in them. The horizontal header piece that contains the days of the week names remains white no matter what I do. Is there a way to change the background color of this header using a style sheet?

Any help would be appreciated.

Thank you.

Upvotes: 3

Views: 4625

Answers (2)

Michael Starke
Michael Starke

Reputation: 316

I've not tested it with versions below 4.8 but we had the same problem and the solution was quite simple. We used this CSS code:

QWidget#qt_calendar_navigationbar
{
    background-color: #424242;
    border: 1px solid #4f4f4f;
}

edit: Well, read before you post - I do not know if it works in your subclass, but it may be worth a try.

Upvotes: 6

Ryan
Ryan

Reputation: 84

I assume that you are trying to use a .qss file and setting the stylesheet using that file.

The developers did not fully implement the use of external stylesheets with the QCalendarWidget so you will have to hack it a bit.

I would suggest that you added a constant to the .qss file that you are using something like:

@cons BACKGROUND_COLOR: cyan

Then you can read from the file in your code:

String color = "";
try {
    URL qssFile = getClass().getResource("*PATHNAME*");
    Scanner scanner = new Scanner(qssFile.openStream());
    String nextLine;
    try {
        while (scanner.hasNextLine()){
        nextLine = scanner.nextLine();
        if (nextLine.contains("BACKGROUND_COLOR:")) {
            color = nextLine.substring(nextLine.indexOf("BACKGROUND_COLOR:") + 17);
        }
    }
    finally{
        scanner.close();
    }
} catch (IOException e) {
    e.printStackTrace();
}

The 17 in the code refers to the length of BACKGROUND_COLOR: so we can get the text after it.

Next you will want to create a new QColor, setting it to the variable color from above. Next create a new QBrush from the QColor. Then create a new QTextCharFormat and set its background to be the QBrush. Finally, set the format by calling the setWeekdayTextFormat method on the QCalendarWidget, passing it the days of the week you wish to change and the format you want to change it to. The follow code sets the box for every day of the week in the HorizontalHeader to have a background color of cyan:

QColor c = new QColor(color);
QBrush b = new QBrush(c);
QTextCharFormat format = new QTextCharFormat();
format.setBackground(b);
this.setWeekdayTextFormat(Qt.DayOfWeek.Sunday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Monday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Tuesday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Wednesday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Thursday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Friday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Saturday, format);

Use all of that code together and you have yourself a way to change the HorizontalHeader background color by using .qss files (and more if you wish).

Upvotes: 2

Related Questions