How to create a custom calender widget: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[How to create a custom calender widget Bulgarian|Български]]
[[Category:HowTo]]<br />[[Category:Snippets]]


=Customizing the appearance of QCalendarWidget=
'''English''' | [[How_to_create_a_custom_calender_widget_German|Deutsch]] | [[How_to_create_a_custom_calender_widget_Bulgarian|Български]]


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


Inheriting the [http://doc.qt.io/qt-5.0/qtwidgets/qcalendarwidget.html QCalendarWidget] ''[qt.io]'' can solve the issue. Here is the example class which explains a ‘custom’ calendar widget.
There are numerous ways to make a calendar. And the simplest one , must be using the &quot;QCalendarWidget&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qcalendarwidget.html. However, we have limited control over the appearance of this widget.


The cells, or the particular days are customized, and in order to do this , we need to take control over the protected function [http://doc.qt.io/qt-5.0/qtwidgets/qcalendarwidget.html#paintCell paintCell] ''[qt.io]''
Inheriting the &quot;QCalendarWidget&amp;quot;: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 &quot;paintCell&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qcalendarwidget.html#paintCell


'''Example:'''
'''Example:'''


That is all. Happy coding.
<code>class ourCalendarWidget : public QCalendarWidget<br />{
 
Q_OBJECT
 
public:<br /> ourCalendarWidget(QWidget *parent = 0) : QCalendarWidget(parent){}<br /> ~ourCalendarWidget() {}
 
void ourCall(QDate date)<br />{<br /> // here we set some conditions<br /> update();<br />}
 
protected:<br />void paintCell(QPainter *painter, const QRect &amp;rect, const QDate &amp;date) const<br />{<br /> if ( ) // our conditions<br /> { // When the conditions are matched, passed QDate is drawn as we like.


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


* [[:Category:HowTo|HowTo]]
That is all. Happy coding.
* [[:Category:snippets|snippets]]

Revision as of 10:00, 24 February 2015


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

Customizing the appearance of QCalendarWidget

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

Inheriting the "QCalendarWidget&quot;: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&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qcalendarwidget.html#paintCell

Example:

class ourCalendarWidget : public QCalendarWidget<br />{

Q_OBJECT

public:<br /> ourCalendarWidget(QWidget *parent = 0) : QCalendarWidget(parent){}<br /> ~ourCalendarWidget() {}

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

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

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

That is all. Happy coding.