How to create a custom calender widget: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Decode HTML entity names)
(Cleanup)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Snippets]]
There are numerous ways to make a calendar. And the simplest one , must be using the {{DocLink|QCalendarWidget}}. However, we have limited control over the appearance of this widget.


'''English''' | [[How_to_create_a_custom_calender_widget_German|Deutsch]] | [[How_to_create_a_custom_calender_widget_Bulgarian|Български]]
Inheriting the {{DocLink|QCalendarWidget}} can solve the issue. Here is the example class which explains a 'custom' calendar widget.
 
= Customizing the appearance of QCalendarWidget =
 
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]. However, we have limited control over the appearance of this widget.
 
Inheriting the [http://doc.qt.io/qt-5.0/qtwidgets/qcalendarwidget.html QCalendarWidget] 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 [http://doc.qt.io/qt-5.0/qtwidgets/qcalendarwidget.html#paintCell paintCell]
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]
Line 16: Line 10:
'''Example:'''
'''Example:'''


<code>class ourCalendarWidget : public QCalendarWidget
<code>
class ourCalendarWidget : public QCalendarWidget
{
{
    Q_OBJECT
public:
    ourCalendarWidget(QWidget* parent=0)
        : QCalendarWidget(parent)
    {
    }


Q_OBJECT
    ~ourCalendarWidget()
    {
    }


public:
    void ourCall(QDate date)  
ourCalendarWidget(QWidget *parent = 0) : QCalendarWidget(parent){}
    {
~ourCalendarWidget() {}
        // here we set some conditions
 
        update();
void ourCall(QDate date)
    }
{
// here we set some conditions
update();
}


protected:
protected:
void paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
    void paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
    {
if ( ) // our conditions
        if ( ) { // our conditions
{ // When the conditions are matched, passed QDate is drawn as we like.
            // When the conditions are matched, passed QDate is drawn as we like.
 
            painter->save();
painter->save();
            painter->drawEllipse(rect); // here we draw n ellipse and the day—
painter->drawEllipse(rect); // here we draw n ellipse and the day—
            painter->drawText(rec, Qt::TextSingleLine, Qt::AlignCenter,QString::number(date.day()));
painter->drawText(rec, Qt::TextSingleLine, Qt::AlignCenter,QString::number(date.day()));
            painter->restore();
painter->restore();
        } else { // if our conditions are not matching, show the default way.
}
            QCalendarWidget::paintCell(painter, rect, date);
else
        }
{ // if our conditions are not matching, show the default way.
    }
QCalendarWidget::paintCell(painter, rect, date);
};
}
</code>
}</code>


That is all. Happy coding.
That is all. Happy coding.

Revision as of 18:54, 28 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

There are numerous ways to make a calendar. And the simplest one , must be using the QCalendarWidget. However, we have limited control over the appearance of this widget.

Inheriting the QCalendarWidget 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

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.