<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- calendarwidget.qdoc --> <title>Qt 4.8: Calendar Widget Example</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <div class="breadcrumb toolblock"> <ul> <li class="first"><a href="index.html">Home</a></li> <!-- Breadcrumbs go here --> <li><a href="all-examples.html">Examples</a></li> <li>Calendar Widget Example</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#window-class-definition">Window Class Definition</a></li> <li class="level1"><a href="#window-class-implementation">Window Class Implementation</a></li> </ul> </div> <h1 class="title">Calendar Widget Example</h1> <span class="subtitle"></span> <!-- $$$widgets/calendarwidget-description --> <div class="descr"> <a name="details"></a> <p>Files:</p> <ul> <li><a href="widgets-calendarwidget-window-cpp.html">widgets/calendarwidget/window.cpp</a></li> <li><a href="widgets-calendarwidget-window-h.html">widgets/calendarwidget/window.h</a></li> <li><a href="widgets-calendarwidget-main-cpp.html">widgets/calendarwidget/main.cpp</a></li> <li><a href="widgets-calendarwidget-calendarwidget-pro.html">widgets/calendarwidget/calendarwidget.pro</a></li> </ul> <p>The Calendar Widget example shows use of <tt>QCalendarWidget</tt>.<p class="centerAlign"><img src="images/calendarwidgetexample.png" alt="" /></p><p><a href="qcalendarwidget.html">QCalendarWidget</a> displays one calendar month at a time and lets the user select a date. The calendar consists of four components: a navigation bar that lets the user change the month that is displayed, a grid where each cell represents one day in the month, and two headers that display weekday names and week numbers.</p> <p>The Calendar Widget example displays a <a href="qcalendarwidget.html">QCalendarWidget</a> and lets the user configure its appearance and behavior using <a href="qcombobox.html">QComboBox</a>es, <a href="qcheckbox.html">QCheckBox</a>es, and <a href="qdateedit.html">QDateEdit</a>s. In addition, the user can influence the formatting of individual dates and headers.</p> <p>The properties of the <a href="qcalendarwidget.html">QCalendarWidget</a> are summarized in the table below.</p> <table class="generic"> <thead><tr class="qt-style"><th >Property</th><th >Description</th></tr></thead> <tr valign="top" class="odd"><td ><a href="qcalendarwidget.html#selectedDate-prop">selectedDate</a></td><td >The currently selected date.</td></tr> <tr valign="top" class="even"><td ><a href="qcalendarwidget.html#minimumDate-prop">minimumDate</a></td><td >The earliest date that can be selected.</td></tr> <tr valign="top" class="odd"><td ><a href="qcalendarwidget.html#maximumDate-prop">maximumDate</a></td><td >The latest date that can be selected.</td></tr> <tr valign="top" class="even"><td ><a href="qcalendarwidget.html#firstDayOfWeek-prop">firstDayOfWeek</a></td><td >The day that is displayed as the first day of the week (usually Sunday or Monday).</td></tr> <tr valign="top" class="odd"><td ><a href="qcalendarwidget.html#gridVisible-prop">gridVisible</a></td><td >Whether the grid should be shown.</td></tr> <tr valign="top" class="even"><td ><a href="qcalendarwidget.html#selectionMode-prop">selectionMode</a></td><td >Whether the user can select a date or not.</td></tr> <tr valign="top" class="odd"><td ><a href="qcalendarwidget.html#horizontalHeaderFormat-prop">horizontalHeaderFormat</a></td><td >The format of the day names in the horizontal header (e.g., "M", "Mon", or "Monday").</td></tr> <tr valign="top" class="even"><td ><a href="qcalendarwidget.html#verticalHeaderFormat-prop">verticalHeaderFormat</a></td><td >The format of the vertical header.</td></tr> <tr valign="top" class="odd"><td ><a href="qcalendarwidget.html#navigationBarVisible-prop">navigationBarVisible</a></td><td >Whether the navigation bar at the top of the calendar widget is shown.</td></tr> </table> <p>The example consists of one class, <tt>Window</tt>, which creates and lays out the <a href="qcalendarwidget.html">QCalendarWidget</a> and the other widgets that let the user configure the <a href="qcalendarwidget.html">QCalendarWidget</a>.</p> <a name="window-class-definition"></a> <h2>Window Class Definition</h2> <p>Here is the definition of the <tt>Window</tt> class:</p> <pre class="cpp"> <span class="keyword">class</span> Window : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span> { Q_OBJECT <span class="keyword">public</span>: Window(); <span class="keyword">private</span> <span class="keyword">slots</span>: <span class="type">void</span> localeChanged(<span class="type">int</span> index); <span class="type">void</span> firstDayChanged(<span class="type">int</span> index); <span class="type">void</span> selectionModeChanged(<span class="type">int</span> index); <span class="type">void</span> horizontalHeaderChanged(<span class="type">int</span> index); <span class="type">void</span> verticalHeaderChanged(<span class="type">int</span> index); <span class="type">void</span> selectedDateChanged(); <span class="type">void</span> minimumDateChanged(<span class="keyword">const</span> <span class="type"><a href="qdate.html">QDate</a></span> <span class="operator">&</span>date); <span class="type">void</span> maximumDateChanged(<span class="keyword">const</span> <span class="type"><a href="qdate.html">QDate</a></span> <span class="operator">&</span>date); <span class="type">void</span> weekdayFormatChanged(); <span class="type">void</span> weekendFormatChanged(); <span class="type">void</span> reformatHeaders(); <span class="type">void</span> reformatCalendarPage(); <span class="keyword">private</span>: <span class="type">void</span> createPreviewGroupBox(); <span class="type">void</span> createGeneralOptionsGroupBox(); <span class="type">void</span> createDatesGroupBox(); <span class="type">void</span> createTextFormatsGroupBox(); <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>createColorComboBox(); <span class="type"><a href="qgroupbox.html">QGroupBox</a></span> <span class="operator">*</span>previewGroupBox; <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>previewLayout; <span class="type"><a href="qcalendarwidget.html">QCalendarWidget</a></span> <span class="operator">*</span>calendar; <span class="type"><a href="qgroupbox.html">QGroupBox</a></span> <span class="operator">*</span>generalOptionsGroupBox; <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>localeLabel; <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>firstDayLabel; ... <span class="type"><a href="qcheckbox.html">QCheckBox</a></span> <span class="operator">*</span>mayFirstCheckBox; };</pre> <p>As is often the case with classes that represent self-contained windows, most of the API is private. We will review the private members as we stumble upon them in the implementation.</p> <a name="window-class-implementation"></a> <h2>Window Class Implementation</h2> <p>Let's now review the class implementation, starting with the constructor:</p> <pre class="cpp"> Window<span class="operator">::</span>Window() { createPreviewGroupBox(); createGeneralOptionsGroupBox(); createDatesGroupBox(); createTextFormatsGroupBox(); <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>; layout<span class="operator">-</span><span class="operator">></span>addWidget(previewGroupBox<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>); layout<span class="operator">-</span><span class="operator">></span>addWidget(generalOptionsGroupBox<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span>); layout<span class="operator">-</span><span class="operator">></span>addWidget(datesGroupBox<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span>); layout<span class="operator">-</span><span class="operator">></span>addWidget(textFormatsGroupBox<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>); layout<span class="operator">-</span><span class="operator">></span>setSizeConstraint(<span class="type"><a href="qlayout.html">QLayout</a></span><span class="operator">::</span>SetFixedSize); setLayout(layout); previewLayout<span class="operator">-</span><span class="operator">></span>setRowMinimumHeight(<span class="number">0</span><span class="operator">,</span> calendar<span class="operator">-</span><span class="operator">></span>sizeHint()<span class="operator">.</span>height()); previewLayout<span class="operator">-</span><span class="operator">></span>setColumnMinimumWidth(<span class="number">0</span><span class="operator">,</span> calendar<span class="operator">-</span><span class="operator">></span>sizeHint()<span class="operator">.</span>width()); setWindowTitle(tr(<span class="string">"Calendar Widget"</span>)); }</pre> <p>We start by creating the four <a href="qgroupbox.html">QGroupBox</a>es and their child widgets (including the <a href="qcalendarwidget.html">QCalendarWidget</a>) using four private <tt>create...GroupBox()</tt> functions, described below. Then we arrange the group boxes in a <a href="qgridlayout.html">QGridLayout</a>.</p> <p>We set the grid layout's resize policy to <a href="qlayout.html#SizeConstraint-enum">QLayout::SetFixedSize</a> to prevent the user from resizing the window. In that mode, the window's size is set automatically by <a href="qgridlayout.html">QGridLayout</a> based on the size hints of its contents widgets.</p> <p>To ensure that the window isn't automatically resized every time we change a property of the <a href="qcalendarwidget.html">QCalendarWidget</a> (e.g., hiding the navigation bar, trhe vertical header, or the grid), we set the minimum height of row 0 and the minimum width of column 0 to the initial size of the <a href="qcalendarwidget.html">QCalendarWidget</a>.</p> <p>Let's move on to the <tt>createPreviewGroupBox()</tt> function:</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>createPreviewGroupBox() { previewGroupBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgroupbox.html">QGroupBox</a></span>(tr(<span class="string">"Preview"</span>)); calendar <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcalendarwidget.html">QCalendarWidget</a></span>; calendar<span class="operator">-</span><span class="operator">></span>setMinimumDate(<span class="type"><a href="qdate.html">QDate</a></span>(<span class="number">1900</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>)); calendar<span class="operator">-</span><span class="operator">></span>setMaximumDate(<span class="type"><a href="qdate.html">QDate</a></span>(<span class="number">3000</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>)); calendar<span class="operator">-</span><span class="operator">></span>setGridVisible(<span class="keyword">true</span>); connect(calendar<span class="operator">,</span> SIGNAL(currentPageChanged(<span class="type">int</span><span class="operator">,</span><span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(reformatCalendarPage())); previewLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>; previewLayout<span class="operator">-</span><span class="operator">></span>addWidget(calendar<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>AlignCenter); previewGroupBox<span class="operator">-</span><span class="operator">></span>setLayout(previewLayout); }</pre> <p>The <b>Preview</b> group box contains only one widget: the <a href="qcalendarwidget.html">QCalendarWidget</a>. We set it up, connect its <a href="qcalendarwidget.html#currentPageChanged">currentPageChanged()</a> signal to our <tt>reformatCalendarPage()</tt> slot to make sure that every new page gets the formatting specified by the user.</p> <p>The <tt>createGeneralOptionsGroupBox()</tt> function is somewhat large and several widgets are set up the same way; we look at parts of its implementation here and skip the rest:</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>createGeneralOptionsGroupBox() { generalOptionsGroupBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgroupbox.html">QGroupBox</a></span>(tr(<span class="string">"General Options"</span>)); localeCombo <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>; <span class="type">int</span> curLocaleIndex <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>; <span class="type">int</span> index <span class="operator">=</span> <span class="number">0</span>; <span class="keyword">for</span> (<span class="type">int</span> _lang <span class="operator">=</span> <span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>C; _lang <span class="operator"><</span><span class="operator">=</span> <span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>LastLanguage; <span class="operator">+</span><span class="operator">+</span>_lang) { <span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>Language lang <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator"><</span><span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>Language<span class="operator">></span>(_lang); <span class="type"><a href="qlist.html">QList</a></span><span class="operator"><</span><span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>Country<span class="operator">></span> countries <span class="operator">=</span> <span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>countriesForLanguage(lang); <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> countries<span class="operator">.</span>count(); <span class="operator">+</span><span class="operator">+</span>i) { <span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>Country country <span class="operator">=</span> countries<span class="operator">.</span>at(i); <span class="type"><a href="qstring.html">QString</a></span> label <span class="operator">=</span> <span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>languageToString(lang); label <span class="operator">+</span><span class="operator">=</span> QLatin1Char(<span class="char">'/'</span>); label <span class="operator">+</span><span class="operator">=</span> <span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>countryToString(country); <span class="type"><a href="qlocale.html">QLocale</a></span> locale(lang<span class="operator">,</span> country); <span class="keyword">if</span> (<span class="keyword">this</span><span class="operator">-</span><span class="operator">></span>locale()<span class="operator">.</span>language() <span class="operator">=</span><span class="operator">=</span> lang <span class="operator">&</span><span class="operator">&</span> <span class="keyword">this</span><span class="operator">-</span><span class="operator">></span>locale()<span class="operator">.</span>country() <span class="operator">=</span><span class="operator">=</span> country) curLocaleIndex <span class="operator">=</span> index; localeCombo<span class="operator">-</span><span class="operator">></span>addItem(label<span class="operator">,</span> locale); <span class="operator">+</span><span class="operator">+</span>index; } } <span class="keyword">if</span> (curLocaleIndex <span class="operator">!</span><span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>) localeCombo<span class="operator">-</span><span class="operator">></span>setCurrentIndex(curLocaleIndex); localeLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"&Locale"</span>)); localeLabel<span class="operator">-</span><span class="operator">></span>setBuddy(localeCombo); firstDayCombo <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>; firstDayCombo<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Sunday"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Sunday); firstDayCombo<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Monday"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Monday); firstDayCombo<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Tuesday"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Tuesday); firstDayCombo<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Wednesday"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Wednesday); firstDayCombo<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Thursday"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Thursday); firstDayCombo<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Friday"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Friday); firstDayCombo<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Saturday"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Saturday); firstDayLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"Wee&k starts on:"</span>)); firstDayLabel<span class="operator">-</span><span class="operator">></span>setBuddy(firstDayCombo); ...</pre> <p>We start with the setup of the <b>Week starts on</b> combobox. This combobox controls which day should be displayed as the first day of the week.</p> <p>The <a href="qcombobox.html">QComboBox</a> class lets us attach user data as a <a href="qvariant.html">QVariant</a> to each item. The data can later be retrieved with <a href="qcombobox.html">QComboBox</a>'s <a href="qcombobox.html#itemData">itemData()</a> function. <a href="qvariant.html">QVariant</a> doesn't directly support the <a href="qt.html#DayOfWeek-enum">Qt::DayOfWeek</a> data type, but it supports <tt>int</tt>, and C++ will happily convert any enum value to <tt>int</tt>.</p> <pre class="qml"> ... connect(localeCombo<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(localeChanged(<span class="type">int</span>))); connect(firstDayCombo<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(firstDayChanged(<span class="type">int</span>))); connect(selectionModeCombo<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(selectionModeChanged(<span class="type">int</span>))); connect(gridCheckBox<span class="operator">,</span> SIGNAL(toggled(<span class="type">bool</span>))<span class="operator">,</span> calendar<span class="operator">,</span> SLOT(setGridVisible(<span class="type">bool</span>))); connect(navigationCheckBox<span class="operator">,</span> SIGNAL(toggled(<span class="type">bool</span>))<span class="operator">,</span> calendar<span class="operator">,</span> SLOT(setNavigationBarVisible(<span class="type">bool</span>))); connect(horizontalHeaderCombo<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(horizontalHeaderChanged(<span class="type">int</span>))); connect(verticalHeaderCombo<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(verticalHeaderChanged(<span class="type">int</span>))); ...</pre> <p>After creating the widgets, we connect the signals and slots. We connect the comboboxes to private slots of <tt>Window</tt> or to public slots provided by <a href="qcombobox.html">QComboBox</a>.</p> <pre class="qml"> ... firstDayChanged(firstDayCombo<span class="operator">-</span><span class="operator">></span>currentIndex()); selectionModeChanged(selectionModeCombo<span class="operator">-</span><span class="operator">></span>currentIndex()); horizontalHeaderChanged(horizontalHeaderCombo<span class="operator">-</span><span class="operator">></span>currentIndex()); verticalHeaderChanged(verticalHeaderCombo<span class="operator">-</span><span class="operator">></span>currentIndex()); }</pre> <p>At the end of the function, we call the slots that update the calendar to ensure that the <a href="qcalendarwidget.html">QCalendarWidget</a> is synchronized with the other widgets on startup.</p> <p>Let's now take a look at the <tt>createDatesGroupBox()</tt> private function:</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>createDatesGroupBox() { datesGroupBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgroupbox.html">QGroupBox</a></span>(tr(<span class="string">"Dates"</span>)); minimumDateEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdateedit.html">QDateEdit</a></span>; minimumDateEdit<span class="operator">-</span><span class="operator">></span>setDisplayFormat(<span class="string">"MMM d yyyy"</span>); minimumDateEdit<span class="operator">-</span><span class="operator">></span>setDateRange(calendar<span class="operator">-</span><span class="operator">></span>minimumDate()<span class="operator">,</span> calendar<span class="operator">-</span><span class="operator">></span>maximumDate()); minimumDateEdit<span class="operator">-</span><span class="operator">></span>setDate(calendar<span class="operator">-</span><span class="operator">></span>minimumDate()); minimumDateLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"&Minimum Date:"</span>)); minimumDateLabel<span class="operator">-</span><span class="operator">></span>setBuddy(minimumDateEdit); currentDateEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdateedit.html">QDateEdit</a></span>; currentDateEdit<span class="operator">-</span><span class="operator">></span>setDisplayFormat(<span class="string">"MMM d yyyy"</span>); currentDateEdit<span class="operator">-</span><span class="operator">></span>setDate(calendar<span class="operator">-</span><span class="operator">></span>selectedDate()); currentDateEdit<span class="operator">-</span><span class="operator">></span>setDateRange(calendar<span class="operator">-</span><span class="operator">></span>minimumDate()<span class="operator">,</span> calendar<span class="operator">-</span><span class="operator">></span>maximumDate()); currentDateLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"&Current Date:"</span>)); currentDateLabel<span class="operator">-</span><span class="operator">></span>setBuddy(currentDateEdit); maximumDateEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdateedit.html">QDateEdit</a></span>; maximumDateEdit<span class="operator">-</span><span class="operator">></span>setDisplayFormat(<span class="string">"MMM d yyyy"</span>); maximumDateEdit<span class="operator">-</span><span class="operator">></span>setDateRange(calendar<span class="operator">-</span><span class="operator">></span>minimumDate()<span class="operator">,</span> calendar<span class="operator">-</span><span class="operator">></span>maximumDate()); maximumDateEdit<span class="operator">-</span><span class="operator">></span>setDate(calendar<span class="operator">-</span><span class="operator">></span>maximumDate()); maximumDateLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"Ma&ximum Date:"</span>)); maximumDateLabel<span class="operator">-</span><span class="operator">></span>setBuddy(maximumDateEdit);</pre> <p>In this function, we create the <b>Minimum Date</b>, <b>Maximum Date</b>, and <b>Current Date</b> editor widgets, which control the calendar's minimum, maximum, and selected dates. The calendar's minimum and maximum dates have already been set in <tt>createPrivewGroupBox()</tt>; we can then set the widgets default values to the calendars values.</p> <pre class="cpp"> connect(currentDateEdit<span class="operator">,</span> SIGNAL(dateChanged(<span class="type"><a href="qdate.html">QDate</a></span>))<span class="operator">,</span> calendar<span class="operator">,</span> SLOT(setSelectedDate(<span class="type"><a href="qdate.html">QDate</a></span>))); connect(calendar<span class="operator">,</span> SIGNAL(selectionChanged())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(selectedDateChanged())); connect(minimumDateEdit<span class="operator">,</span> SIGNAL(dateChanged(<span class="type"><a href="qdate.html">QDate</a></span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(minimumDateChanged(<span class="type"><a href="qdate.html">QDate</a></span>))); connect(maximumDateEdit<span class="operator">,</span> SIGNAL(dateChanged(<span class="type"><a href="qdate.html">QDate</a></span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(maximumDateChanged(<span class="type"><a href="qdate.html">QDate</a></span>))); ... }</pre> <p>We connect the <tt>currentDateEdit</tt>'s <a href="qdatetimeedit.html#dateChanged">dateChanged()</a> signal directly to the calendar's <a href="qcalendarwidget.html#selectedDate-prop">setSelectedDate()</a> slot. When the calendar's selected date changes, either as a result of a user action or programmatically, our <tt>selectedDateChanged()</tt> slot updates the <b>Current Date</b> editor. We also need to react when the user changes the <b>Minimum Date</b> and <b>Maximum Date</b> editors.</p> <p>Here is the <tt>createTextFormatsGroup()</tt> function:</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>createTextFormatsGroupBox() { textFormatsGroupBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgroupbox.html">QGroupBox</a></span>(tr(<span class="string">"Text Formats"</span>)); weekdayColorCombo <span class="operator">=</span> createColorComboBox(); weekdayColorCombo<span class="operator">-</span><span class="operator">></span>setCurrentIndex( weekdayColorCombo<span class="operator">-</span><span class="operator">></span>findText(tr(<span class="string">"Black"</span>))); weekdayColorLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"&Weekday color:"</span>)); weekdayColorLabel<span class="operator">-</span><span class="operator">></span>setBuddy(weekdayColorCombo); weekendColorCombo <span class="operator">=</span> createColorComboBox(); weekendColorCombo<span class="operator">-</span><span class="operator">></span>setCurrentIndex( weekendColorCombo<span class="operator">-</span><span class="operator">></span>findText(tr(<span class="string">"Red"</span>))); weekendColorLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"Week&end color:"</span>)); weekendColorLabel<span class="operator">-</span><span class="operator">></span>setBuddy(weekendColorCombo);</pre> <p>We set up the <b>Weekday Color</b> and <b>Weekend Color</b> comboboxes using <tt>createColorCombo()</tt>, which instantiates a <a href="qcombobox.html">QComboBox</a> and populates it with colors ("Red", "Blue", etc.).</p> <pre class="cpp"> headerTextFormatCombo <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>; headerTextFormatCombo<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Bold"</span>)); headerTextFormatCombo<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Italic"</span>)); headerTextFormatCombo<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Plain"</span>)); headerTextFormatLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"&Header text:"</span>)); headerTextFormatLabel<span class="operator">-</span><span class="operator">></span>setBuddy(headerTextFormatCombo); firstFridayCheckBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcheckbox.html">QCheckBox</a></span>(tr(<span class="string">"&First Friday in blue"</span>)); mayFirstCheckBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcheckbox.html">QCheckBox</a></span>(tr(<span class="string">"May &1 in red"</span>));</pre> <p>The <b>Header Text Format</b> combobox lets the user change the text format (bold, italic, or plain) used for horizontal and vertical headers. The <b>First Friday in blue</b> and <b>May 1 in red</b> check box affect the rendering of specific dates.</p> <pre class="cpp"> connect(weekdayColorCombo<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(weekdayFormatChanged())); connect(weekendColorCombo<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(weekendFormatChanged())); connect(headerTextFormatCombo<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type"><a href="qstring.html">QString</a></span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(reformatHeaders())); connect(firstFridayCheckBox<span class="operator">,</span> SIGNAL(toggled(<span class="type">bool</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(reformatCalendarPage())); connect(mayFirstCheckBox<span class="operator">,</span> SIGNAL(toggled(<span class="type">bool</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(reformatCalendarPage()));</pre> <p>We connect the check boxes and comboboxes to various private slots. The <b>First Friday in blue</b> and <b>May 1 in red</b> check boxes are both connected to <tt>reformatCalendarPage()</tt>, which is also called when the calendar switches month.</p> <pre class="qml"> ... reformatHeaders(); reformatCalendarPage(); }</pre> <p>At the end of <tt>createTextFormatsGroupBox()</tt>, we call private slots to synchronize the <a href="qcalendarwidget.html">QCalendarWidget</a> with the other widgets.</p> <p>We're now done reviewing the four <tt>create...GroupBox()</tt> functions. Let's now take a look at the other private functions and slots.</p> <pre class="cpp"> <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>Window<span class="operator">::</span>createColorComboBox() { <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>comboBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>; comboBox<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Red"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>red); comboBox<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Blue"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>blue); comboBox<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Black"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>black); comboBox<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Magenta"</span>)<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>magenta); <span class="keyword">return</span> comboBox; }</pre> <p>In <tt>createColorCombo()</tt>, we create a combobox and populate it with standard colors. The second argument to <a href="qcombobox.html#addItem">QComboBox::addItem</a>() is a <a href="qvariant.html">QVariant</a> storing user data (in this case, <a href="qcolor.html">QColor</a> objects).</p> <p>This function was used to set up the <b>Weekday Color</b> and <b>Weekend Color</b> comboboxes.</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>firstDayChanged(<span class="type">int</span> index) { calendar<span class="operator">-</span><span class="operator">></span>setFirstDayOfWeek(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>DayOfWeek( firstDayCombo<span class="operator">-</span><span class="operator">></span>itemData(index)<span class="operator">.</span>toInt())); }</pre> <p>When the user changes the <b>Week starts on</b> combobox's value, <tt>firstDayChanged()</tt> is invoked with the index of the combobox's new value. We retrieve the custom data item associated with the new current item using <a href="qcombobox.html#itemData">itemData()</a> and cast it to a <a href="qt.html#DayOfWeek-enum">Qt::DayOfWeek</a>.</p> <p><tt>selectionModeChanged()</tt>, <tt>horizontalHeaderChanged()</tt>, and <tt>verticalHeaderChanged()</tt> are very similar to <tt>firstDayChanged()</tt>, so they are omitted.</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>selectedDateChanged() { currentDateEdit<span class="operator">-</span><span class="operator">></span>setDate(calendar<span class="operator">-</span><span class="operator">></span>selectedDate()); }</pre> <p>The <tt>selectedDateChanged()</tt> updates the <b>Current Date</b> editor to reflect the current state of the <a href="qcalendarwidget.html">QCalendarWidget</a>.</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>minimumDateChanged(<span class="keyword">const</span> <span class="type"><a href="qdate.html">QDate</a></span> <span class="operator">&</span>date) { calendar<span class="operator">-</span><span class="operator">></span>setMinimumDate(date); maximumDateEdit<span class="operator">-</span><span class="operator">></span>setDate(calendar<span class="operator">-</span><span class="operator">></span>maximumDate()); }</pre> <p>When the user changes the minimum date, we tell the QCalenderWidget. We also update the <b>Maximum Date</b> editor, because if the new minimum date is later than the current maximum date, <a href="qcalendarwidget.html">QCalendarWidget</a> will automatically adapt its maximum date to avoid a contradicting state.</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>maximumDateChanged(<span class="keyword">const</span> <span class="type"><a href="qdate.html">QDate</a></span> <span class="operator">&</span>date) { calendar<span class="operator">-</span><span class="operator">></span>setMaximumDate(date); minimumDateEdit<span class="operator">-</span><span class="operator">></span>setDate(calendar<span class="operator">-</span><span class="operator">></span>minimumDate()); }</pre> <p><tt>maximumDateChanged()</tt> is implemented similarly to <tt>minimumDateChanged()</tt>.</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>weekdayFormatChanged() { <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> format; format<span class="operator">.</span>setForeground(qvariant_cast<span class="operator"><</span><span class="type"><a href="qcolor.html">QColor</a></span><span class="operator">></span>( weekdayColorCombo<span class="operator">-</span><span class="operator">></span>itemData(weekdayColorCombo<span class="operator">-</span><span class="operator">></span>currentIndex()))); calendar<span class="operator">-</span><span class="operator">></span>setWeekdayTextFormat(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Monday<span class="operator">,</span> format); calendar<span class="operator">-</span><span class="operator">></span>setWeekdayTextFormat(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Tuesday<span class="operator">,</span> format); calendar<span class="operator">-</span><span class="operator">></span>setWeekdayTextFormat(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Wednesday<span class="operator">,</span> format); calendar<span class="operator">-</span><span class="operator">></span>setWeekdayTextFormat(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Thursday<span class="operator">,</span> format); calendar<span class="operator">-</span><span class="operator">></span>setWeekdayTextFormat(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Friday<span class="operator">,</span> format); }</pre> <p>Each combobox item has a <a href="qcolor.html">QColor</a> object as user data corresponding to the item's text. After fetching the colors from the comboboxes, we set the text format of each day of the week.</p> <p>The text format of a column in the calendar is given as a <a href="qtextcharformat.html">QTextCharFormat</a>, which besides the foreground color lets us specify various character formatting information. In this example, we only show a subset of the possibilities.</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>weekendFormatChanged() { <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> format; format<span class="operator">.</span>setForeground(qvariant_cast<span class="operator"><</span><span class="type"><a href="qcolor.html">QColor</a></span><span class="operator">></span>( weekendColorCombo<span class="operator">-</span><span class="operator">></span>itemData(weekendColorCombo<span class="operator">-</span><span class="operator">></span>currentIndex()))); calendar<span class="operator">-</span><span class="operator">></span>setWeekdayTextFormat(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Saturday<span class="operator">,</span> format); calendar<span class="operator">-</span><span class="operator">></span>setWeekdayTextFormat(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Sunday<span class="operator">,</span> format); }</pre> <p><tt>weekendFormatChanged()</tt> is the same as <tt>weekdayFormatChanged()</tt>, except that it affects Saturday and Sunday instead of Monday to Friday.</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>reformatHeaders() { <span class="type"><a href="qstring.html">QString</a></span> text <span class="operator">=</span> headerTextFormatCombo<span class="operator">-</span><span class="operator">></span>currentText(); <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> format; <span class="keyword">if</span> (text <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">"Bold"</span>)) { format<span class="operator">.</span>setFontWeight(<span class="type"><a href="qfont.html">QFont</a></span><span class="operator">::</span>Bold); } <span class="keyword">else</span> <span class="keyword">if</span> (text <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">"Italic"</span>)) { format<span class="operator">.</span>setFontItalic(<span class="keyword">true</span>); } <span class="keyword">else</span> <span class="keyword">if</span> (text <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">"Green"</span>)) { format<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>green); } calendar<span class="operator">-</span><span class="operator">></span>setHeaderTextFormat(format); }</pre> <p>The <tt>reformatHeaders()</tt> slot is called when the user changes the text format of the headers. We compare the current text of the <b>Header Text Format</b> combobox to determine which format to apply. (An alternative would have been to store <a href="qtextcharformat.html">QTextCharFormat</a> values alongside the combobox items.)</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>reformatCalendarPage() { <span class="keyword">if</span> (firstFridayCheckBox<span class="operator">-</span><span class="operator">></span>isChecked()) { <span class="type"><a href="qdate.html">QDate</a></span> firstFriday(calendar<span class="operator">-</span><span class="operator">></span>yearShown()<span class="operator">,</span> calendar<span class="operator">-</span><span class="operator">></span>monthShown()<span class="operator">,</span> <span class="number">1</span>); <span class="keyword">while</span> (firstFriday<span class="operator">.</span>dayOfWeek() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Friday) firstFriday <span class="operator">=</span> firstFriday<span class="operator">.</span>addDays(<span class="number">1</span>); <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> firstFridayFormat; firstFridayFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>blue); calendar<span class="operator">-</span><span class="operator">></span>setDateTextFormat(firstFriday<span class="operator">,</span> firstFridayFormat); } <span class="comment">//May First in Red takes precedence</span> <span class="keyword">if</span> (mayFirstCheckBox<span class="operator">-</span><span class="operator">></span>isChecked()) { <span class="keyword">const</span> <span class="type"><a href="qdate.html">QDate</a></span> mayFirst(calendar<span class="operator">-</span><span class="operator">></span>yearShown()<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">1</span>); <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> mayFirstFormat; mayFirstFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>red); calendar<span class="operator">-</span><span class="operator">></span>setDateTextFormat(mayFirst<span class="operator">,</span> mayFirstFormat); } }</pre> <p>In <tt>reformatCalendarPage()</tt>, we set the text format of the first Friday in the month and May 1 in the current year. The text formats that are actually used depend on which check boxes are checked.</p> <p><a href="qcalendarwidget.html">QCalendarWidget</a> lets us set the text format of individual dates with the <a href="qcalendarwidget.html#setDateTextFormat">setDateTextFormat()</a>. We chose to set the dates when the calendar page changes, i.e., a new month is displayed. We check which of the <tt>mayFirstCheckBox</tt> and <tt>firstDayCheckBox</tt>, if any, are checked and set the text formats accordingly.</p> </div> <!-- @@@widgets/calendarwidget --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2013 Digia Plc and/or its subsidiaries. Documentation contributions included herein are the copyrights of their respective owners.</p> <br /> <p> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.</p> <p> Documentation sources may be obtained from <a href="http://www.qt-project.org"> www.qt-project.org</a>.</p> <br /> <p> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. <a title="Privacy Policy" href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p> </div> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>