Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Archived:How to create a Qt Calendar Widget

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.

This article demonstrates how to create a Qt calendar widget (QCalendarWidget).

Article Metadata
Code ExampleTested with
Devices(s): N8
Platform(s): Symbian^3/Windows Desktop
Keywords: QCalendarWidget
Created: somnathbanik (29 May 2011)
Last edited: hamishwillee (11 Oct 2012)


Basic Idea

QCalendarWidget displays one calendar in month view and allows the user to select a date. The calendar widget has a navigation bar that allows the user to change the month, the dates are displayed in gird view and user can select one date. The top header represents the weekdays and the left header represents the week number in the year.

Calendar Widget

Class Definition

#include <QWidget>
#include "QGroupBox"
#include "QCalendarWidget"
#include "QHBoxLayout"
namespace Ui {
class CalendarQt;
class QCalendarWidget;
class QGroupBox;
class QHBoxLayout;
class CalendarQt : public QWidget
explicit CalendarQt(QWidget *parent = 0);
Ui::CalendarQt *ui;
void CreateCalendarWidget();
QGroupBox *calendarGroupBox;
QCalendarWidget *calendar;
QHBoxLayout *calendarLayout;
#endif // CALENDARQT_H

Class Implementation

Lets start with the constructor .

CalendarQt::CalendarQt(QWidget *parent) :
ui(new Ui::CalendarQt)
QGridLayout *layout = new QGridLayout;
this-> setLayout(layout);

We create the calendar widget using the private function CreateCalendarWidget() and then arrange it to QGridLayout, and then set the QGridLayout to the main widget. setWindowTitle() is used to set the title of the main widget. Before we add the calendar widget to the QGridLayout in constructor, lets move on to the CreateCalendarWidget() function to understand how the calendar widget is being created.

void CalendarQt::CreateCalendarWidget()
calendarGroupBox = new QGroupBox(tr("Widget"));
calendar = new QCalendarWidget;
calendar->setMinimumDate(QDate(1947, 1, 1));
calendar->setMaximumDate(QDate(9999, 1, 1));
connect(calendar, SIGNAL(currentPageChanged(int,int)),
this, SLOT(reformatCalendarPage()));
calendarLayout = new QHBoxLayout;

TheQGroupBox contains only one widget i.e.QCalendarWidget. We set the QCalendarWidget to connect its currentPageChanged() signal to reformatCalendarPage() slot to make sure that every new page gets the formatting specified by the user. We create a QHBoxLayout , add the QCalendarWidget to the QHBoxLayout, and then set the QHBoxLayout to the QGroupBox. The max and min date are set with QCalendarWidget:: setMaximumDate() and QCalendarWidget:: setMinimumDate() functions.

Source Code

The full source code for this article is available here:

This page was last modified on 11 October 2012, at 01:14.
64 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.