How to create a custom calender widget

From Qt Wiki
Revision as of 15:59, 3 March 2015 by AutoSpider (talk | contribs) (Add "cleanup" tag)
Jump to navigation Jump to search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

English | Deutsch | Български

Customizing the appearance of QCalendarWidget

There are numerous ways to make a calendar. And the simplest one , must be using the "QCalendarWidget":http://doc.qt.io/qt-5.0/qtwidgets/qcalendarwidget.html. However, we have limited control over the appearance of this widget.

Inheriting the "QCalendarWidget":http://doc.qt.io/qt-5.0/qtwidgets/qcalendarwidget.html can solve the issue. Here is the example class which explains a 'custom' calendar widget.

The cells, or the particular days are customized, and in order to do this , we need to take control over the protected function "paintCell":http://doc.qt.io/qt-5.0/qtwidgets/qcalendarwidget.html#paintCell

Example:

class ourCalendarWidget : public QCalendarWidget
{

Q_OBJECT

public:
 ourCalendarWidget(QWidget *parent = 0) : QCalendarWidget(parent){}
 ~ourCalendarWidget() {}

void ourCall(QDate date)
{
 // here we set some conditions
 update();
}

protected:
void paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
 if ( ) // our conditions
 { // When the conditions are matched, passed QDate is drawn as we like.

painter->save();
 painter->drawEllipse(rect); // here we draw n ellipse and the day—
 painter->drawText(rec, Qt::TextSingleLine, Qt::AlignCenter,QString::number(date.day()));
 painter->restore();
 }
 else
 { // if our conditions are not matching, show the default way.
 QCalendarWidget::paintCell(painter, rect, date);
 }
}

That is all. Happy coding.