How to create a Custom Calendar: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]
[[Category:Developing with Qt::General]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Tutorial]]


'''English''' [[How_to_Create_a_Custom_Calendar_Bulgarian|Български]]
'''English''' [[How_to_Create_a_Custom_Calendar_Bulgarian|Български]]
Line 5: Line 8:
== Pre-Requisites ==
== Pre-Requisites ==


* &quot;Basic_Qt_Programming_Tutorial&amp;quot;:http://wiki.qt.io/Basic_Qt_Programming_Tutorial
* "Basic_Qt_Programming_Tutorial":http://wiki.qt.io/Basic_Qt_Programming_Tutorial


This calendar widget is a '''Composite widget''' comprising of a QToolButton,QLabel and QCalendarWidget.
This calendar widget is a '''Composite widget''' comprising of a QToolButton,QLabel and QCalendarWidget.
Line 24: Line 27:
open the calendardialog.ui, from the Designer view, Drag and drop the following controls.
open the calendardialog.ui, from the Designer view, Drag and drop the following controls.


Four tool buttons. '''yearBackButton''', '''yearFrontButton''', '''MonthBackButton''', '''MonthFrontButton'''<br />Two labels, '''Titlelabel''', '''DateLabel''',<br />Three '''horizontal spacers''',<br />One Button '''OkButton''', and a calendar widget. '''calendarWidget'''
Four tool buttons. '''yearBackButton''', '''yearFrontButton''', '''MonthBackButton''', '''MonthFrontButton'''
Two labels, '''Titlelabel''', '''DateLabel''',
Three '''horizontal spacers''',
One Button '''OkButton''', and a calendar widget. '''calendarWidget'''


Now open the calendardialog.h and edit,
Now open the calendardialog.h and edit,
Line 32: Line 38:
'''calendardialog.h'''
'''calendardialog.h'''


<code><br />#ifndef CALENDARDIALOG_H<br />#define CALENDARDIALOG_H
<code>
#ifndef CALENDARDIALOG_H
#define CALENDARDIALOG_H


#include &quot;ui_calendardialog.h&amp;quot;
#include "ui_calendardialog.h"


class Calendar : public QDialog<br />{<br /> Q_OBJECT
class Calendar : public QDialog
{
Q_OBJECT


public:<br /> explicit Calendar(QWidget *parent = 0);<br /> private:<br /> Ui::calendarDialog ui;
public:
explicit Calendar(QWidget *parent = 0);
private:
Ui::calendarDialog ui;


private:<br /> QDate getdate();<br /> QDate currentDate() const;<br /> void setCurrentDate(const QDate &amp;d);<br /> void on_calendar_clicked(const QDate &amp;date);
private:
QDate getdate();
QDate currentDate() const;
void setCurrentDate(const QDate &amp;d);
void on_calendar_clicked(const QDate &amp;date);


private slots:
private slots:


void on_MonthFrontButton_clicked();<br /> void on_MonthBackButton_clicked();
void on_MonthFrontButton_clicked();
void on_MonthBackButton_clicked();


void on_yearBackButton_clicked();<br /> void on_yearFrontButton_clicked();
void on_yearBackButton_clicked();
void on_yearFrontButton_clicked();


void on_OkButton_clicked();<br />};
void on_OkButton_clicked();
};


#endif // CALENDARDIALOG_H
#endif // CALENDARDIALOG_H
Line 60: Line 80:
<code>
<code>


#include &lt;QCalendarWidget&amp;gt;<br />#include &lt;QDate&amp;gt;<br />#include &lt;QApplication&amp;gt;<br />#include &lt;QMessageBox&amp;gt;
#include <QCalendarWidget>
#include <QDate>
#include <QApplication>
#include <QMessageBox>


#include &quot;calendardialog.h&amp;quot;
#include "calendardialog.h"


Calendar::Calendar(QWidget *parent) :<br /> QDialog(parent)<br />{<br /> ui.setupUi(this);<br /> setWindowFlags(Qt::FramelessWindowHint);<br /> ui.calendarWidget-&gt;setNavigationBarVisible(false);<br /> ui.calendarWidget-&gt;setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames);<br /> ui.calendarWidget-&gt;setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
Calendar::Calendar(QWidget *parent) :
QDialog(parent)
{
ui.setupUi(this);
setWindowFlags(Qt::FramelessWindowHint);
ui.calendarWidget->setNavigationBarVisible(false);
ui.calendarWidget->setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames);
ui.calendarWidget->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);


QObject::connect(ui.yearBackButton,SIGNAL (clicked()),this,SLOT (on_yearBackButton_clicked()));<br /> QObject::connect(ui.yearFrontButton,SIGNAL (clicked()),this,SLOT (on_yearFrontButton_clicked()));<br /> QObject::connect(ui.MonthBackButton,SIGNAL (clicked()),this,SLOT (on_MonthBackButton_clicked()));<br /> QObject::connect(ui.MonthFrontButton,SIGNAL (clicked()),this,SLOT (on_MonthFrontButton_clicked()));
QObject::connect(ui.yearBackButton,SIGNAL (clicked()),this,SLOT (on_yearBackButton_clicked()));
QObject::connect(ui.yearFrontButton,SIGNAL (clicked()),this,SLOT (on_yearFrontButton_clicked()));
QObject::connect(ui.MonthBackButton,SIGNAL (clicked()),this,SLOT (on_MonthBackButton_clicked()));
QObject::connect(ui.MonthFrontButton,SIGNAL (clicked()),this,SLOT (on_MonthFrontButton_clicked()));


QObject::connect(ui.OkButton,SIGNAL (clicked()),this,SLOT (on_OkButton_clicked()));
QObject::connect(ui.OkButton,SIGNAL (clicked()),this,SLOT (on_OkButton_clicked()));
Line 72: Line 105:
}
}


QDate Calendar::currentDate() const {<br /> return ui.calendarWidget-&gt;selectedDate();<br />}<br />void Calendar::setCurrentDate(const QDate &amp;d) {<br /> QDate date = d;<br /> if (!date.isValid())<br /> date = QDate::currentDate();<br /> ui.calendarWidget-&gt;setSelectedDate(date);<br /> ui.calendarWidget-&gt;showSelectedDate();<br /> on_calendar_clicked(date);<br />}
QDate Calendar::currentDate() const {
return ui.calendarWidget->selectedDate();
}
void Calendar::setCurrentDate(const QDate &amp;d) {
QDate date = d;
if (!date.isValid())
date = QDate::currentDate();
ui.calendarWidget->setSelectedDate(date);
ui.calendarWidget->showSelectedDate();
on_calendar_clicked(date);
}


void Calendar::on_calendar_clicked(const QDate &amp;date) {<br /> ui.DateLabel-&gt;setText(date.toString(&quot;dd MMMM yyyy&amp;quot;));<br />}
void Calendar::on_calendar_clicked(const QDate &amp;date) {
ui.DateLabel->setText(date.toString("dd MMMM yyyy"));
}


QDate Calendar::getdate ()<br />{<br /> QMessageBox showDateMessage(QMessageBox::Warning,<br /> ui.calendarWidget-&gt;selectedDate().toString(&quot;dd MMMM yyyy&amp;quot;),&quot;&quot;,QMessageBox::Ok,0);<br /> ui.DateLabel-&gt;setText(ui.calendarWidget-&gt;selectedDate().toString(&quot;dd MMMM yyyy&amp;quot;));<br /> showDateMessage.setText(&quot;Date Selected is &quot;+ui.calendarWidget-&gt;selectedDate().toString(&quot;dd MMMM yyyy&amp;quot;));<br /> int ret = showDateMessage.exec&amp;amp;#40;&amp;#41;;<br /> if(ret == QMessageBox::Ok)<br /> {
QDate Calendar::getdate ()
{
QMessageBox showDateMessage(QMessageBox::Warning,
ui.calendarWidget->selectedDate().toString("dd MMMM yyyy"),"",QMessageBox::Ok,0);
ui.DateLabel->setText(ui.calendarWidget->selectedDate().toString("dd MMMM yyyy"));
showDateMessage.setText("Date Selected is "+ui.calendarWidget->selectedDate().toString("dd MMMM yyyy"));
int ret = showDateMessage.exec();
if(ret == QMessageBox::Ok)
{


accept();<br /> return ui.calendarWidget-&gt;selectedDate();
accept();
return ui.calendarWidget->selectedDate();


}<br /> else<br /> {<br /> return QDate();<br /> }
}
else
{
return QDate();
}


}<br />void Calendar::on_yearBackButton_clicked() {<br /> QDate date = currentDate();
}
void Calendar::on_yearBackButton_clicked() {
QDate date = currentDate();


date = date.addYears(–1);<br /> setCurrentDate(date);<br />}
date = date.addYears(–1);
setCurrentDate(date);
}


void Calendar::on_yearFrontButton_clicked() {<br /> QDate date = currentDate();<br /> date = date.addYears(+1);<br /> setCurrentDate(date);<br />}
void Calendar::on_yearFrontButton_clicked() {
QDate date = currentDate();
date = date.addYears(+1);
setCurrentDate(date);
}


void Calendar::on_MonthFrontButton_clicked() {<br /> QDate date = currentDate();<br /> date = date.addMonths(+1);<br /> setCurrentDate(date);<br />}
void Calendar::on_MonthFrontButton_clicked() {
QDate date = currentDate();
date = date.addMonths(+1);
setCurrentDate(date);
}


void Calendar::on_MonthBackButton_clicked()<br />{<br /> QDate date = currentDate();<br /> date = date.addMonths(–1);<br /> setCurrentDate(date);<br />}
void Calendar::on_MonthBackButton_clicked()
{
QDate date = currentDate();
date = date.addMonths(–1);
setCurrentDate(date);
}


void Calendar::on_OkButton_clicked()<br />{<br /> getdate();<br />}
void Calendar::on_OkButton_clicked()
{
getdate();
}


</code>
</code>
Line 100: Line 178:
'''calendardialog.ui'''
'''calendardialog.ui'''


<code><br />&amp;lt;?xml version=&quot;1.0&amp;quot; encoding=&quot;UTF-8&amp;quot;?&amp;gt;<br />&lt;ui version=&quot;4.0&amp;quot;&gt;<br /> &lt;class&amp;gt;calendarDialog&amp;lt;/class&amp;gt;<br /> &lt;widget class=&quot;QDialog&amp;quot; name=&quot;calendarDialog&amp;quot;&gt;<br /> &lt;property name=&quot;geometry&amp;quot;&gt;<br /> &lt;rect&amp;gt;<br /> &lt;x&amp;gt;0&amp;lt;/x&amp;gt;<br /> &lt;y&amp;gt;0&amp;lt;/y&amp;gt;<br /> &lt;width&amp;gt;400&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;300&amp;lt;/height&amp;gt;<br /> &lt;/rect&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;windowTitle&amp;quot;&gt;<br /> &lt;string&amp;gt;calendarDialog&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;layout class=&quot;QGridLayout&amp;quot; name=&quot;gridLayout&amp;quot;&gt;<br /> &lt;item row=&quot;0&amp;quot; column=&quot;0&amp;quot; colspan=&quot;2&amp;quot;&gt;<br /> &lt;widget class=&quot;QLabel&amp;quot; name=&quot;Titlelabel&amp;quot;&gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;Date Selector&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;alignment&amp;quot;&gt;<br /> &lt;set&amp;gt;Qt::AlignCenter&amp;lt;/set&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item row=&quot;1&amp;quot; column=&quot;0&amp;quot; colspan=&quot;2&amp;quot;&gt;<br /> &lt;layout class=&quot;QHBoxLayout&amp;quot; name=&quot;horizontalLayout&amp;quot;&gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QToolButton&amp;quot; name=&quot;yearBackButton&amp;quot;&gt;<br /> &lt;property name=&quot;minimumSize&amp;quot;&gt;<br /> &lt;size&amp;gt;<br /> &lt;width&amp;gt;30&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;0&amp;lt;/height&amp;gt;<br /> &lt;/size&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;&amp;lt;&amp;lt;&lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;spacer name=&quot;horizontalSpacer&amp;quot;&gt;<br /> &lt;property name=&quot;orientation&amp;quot;&gt;<br /> &lt;enum&amp;gt;Qt::Horizontal&amp;lt;/enum&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;sizeType&amp;quot;&gt;<br /> &lt;enum&amp;gt;QSizePolicy::Fixed&amp;lt;/enum&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;sizeHint&amp;quot; stdset=&quot;0&amp;quot;&gt;<br /> &lt;size&amp;gt;<br /> &lt;width&amp;gt;20&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;20&amp;lt;/height&amp;gt;<br /> &lt;/size&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/spacer&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QToolButton&amp;quot; name=&quot;MonthBackButton&amp;quot;&gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;&amp;lt;&lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QLabel&amp;quot; name=&quot;DateLabel&amp;quot;&gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string/&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;alignment&amp;quot;&gt;<br /> &lt;set&amp;gt;Qt::AlignCenter&amp;lt;/set&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QToolButton&amp;quot; name=&quot;MonthFrontButton&amp;quot;&gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;&amp;gt;&lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;spacer name=&quot;horizontalSpacer_2&amp;quot;&gt;<br /> &lt;property name=&quot;orientation&amp;quot;&gt;<br /> &lt;enum&amp;gt;Qt::Horizontal&amp;lt;/enum&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;sizeType&amp;quot;&gt;<br /> &lt;enum&amp;gt;QSizePolicy::Fixed&amp;lt;/enum&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;sizeHint&amp;quot; stdset=&quot;0&amp;quot;&gt;<br /> &lt;size&amp;gt;<br /> &lt;width&amp;gt;20&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;20&amp;lt;/height&amp;gt;<br /> &lt;/size&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/spacer&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QToolButton&amp;quot; name=&quot;yearFrontButton&amp;quot;&gt;<br /> &lt;property name=&quot;minimumSize&amp;quot;&gt;<br /> &lt;size&amp;gt;<br /> &lt;width&amp;gt;30&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;0&amp;lt;/height&amp;gt;<br /> &lt;/size&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;&amp;gt;&amp;gt;&lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;/layout&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item row=&quot;2&amp;quot; column=&quot;0&amp;quot; colspan=&quot;2&amp;quot;&gt;<br /> &lt;widget class=&quot;QCalendarWidget&amp;quot; name=&quot;calendarWidget&amp;quot;/&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item row=&quot;3&amp;quot; column=&quot;0&amp;quot;&gt;<br /> &lt;spacer name=&quot;horizontalSpacer_3&amp;quot;&gt;<br /> &lt;property name=&quot;orientation&amp;quot;&gt;<br /> &lt;enum&amp;gt;Qt::Horizontal&amp;lt;/enum&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;sizeHint&amp;quot; stdset=&quot;0&amp;quot;&gt;<br /> &lt;size&amp;gt;<br /> &lt;width&amp;gt;298&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;20&amp;lt;/height&amp;gt;<br /> &lt;/size&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/spacer&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item row=&quot;3&amp;quot; column=&quot;1&amp;quot;&gt;<br /> &lt;widget class=&quot;QPushButton&amp;quot; name=&quot;OkButton&amp;quot;&gt;<br /> &lt;property name=&quot;maximumSize&amp;quot;&gt;<br /> &lt;size&amp;gt;<br /> &lt;width&amp;gt;150&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;60&amp;lt;/height&amp;gt;<br /> &lt;/size&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;Ok&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;/layout&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;layoutdefault spacing=&quot;6&amp;quot; margin=&quot;11&amp;quot;/&amp;gt;<br /> &lt;resources/&amp;gt;<br /> &lt;connections/&amp;gt;<br />&lt;/ui&amp;gt;
<code>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>calendarDialog</class>
<widget class="QDialog" name="calendarDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>calendarDialog</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="Titlelabel">
<property name="text">
<string>Date Selector</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QToolButton" name="yearBackButton">
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="text">
<string><<</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="MonthBackButton">
<property name="text">
<string><</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="DateLabel">
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="MonthFrontButton">
<property name="text">
<string>></string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="yearFrontButton">
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>>></string>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCalendarWidget" name="calendarWidget"/>
</item>
<item row="3" column="0">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>298</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="1">
<widget class="QPushButton" name="OkButton">
<property name="maximumSize">
<size>
<width>150</width>
<height>60</height>
</size>
</property>
<property name="text">
<string>Ok</string>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>


</code><br />Finally Create an instance of the Calendar class in main.cpp and call show() method before entering the event loop<br />by calling a.exec&amp;amp;#40;&amp;#41;
</code>
Finally Create an instance of the Calendar class in main.cpp and call show() method before entering the event loop
by calling a.exec()


'''main.cpp'''
'''main.cpp'''
Line 108: Line 335:
<code>
<code>


#include &lt;QtGui/QApplication&amp;gt;<br />#include &quot;calendardialog.h&amp;quot;
#include <QtGui/QApplication>
#include "calendardialog.h"
 
int main(int argc, char '''argv[])
{
QApplication a(argc, argv);
Calendar w;
w.show();
 
return a.exec();
}
 
</code>
 
With the above example files one can easily create a custom calendar widget.
 
h2. See also


int main(int argc, char '''argv[])<br />{<br /> QApplication a(argc, argv);<br /> Calendar w;<br /> w.show();
''' "How to use signals and slots":http://developer.qt.nokia.com/wiki/How-to-use-signals-and-slots
<br /> return a.exec&amp;amp;#40;&amp;#41;;<br />}
* "Counting_Clicks_for_Qt_Widgets":http://wiki.qt.io/Counting_Clicks_for_Qt_Widgets
<br /></code>
<br />With the above example files one can easily create a custom calendar widget.
<br />h2. See also
<br />''' &quot;How to use signals and slots&amp;quot;:http://developer.qt.nokia.com/wiki/How-to-use-signals-and-slots<br />* &quot;Counting_Clicks_for_Qt_Widgets&amp;quot;:http://wiki.qt.io/Counting_Clicks_for_Qt_Widgets

Revision as of 10:16, 25 February 2015


English Български

Pre-Requisites

This calendar widget is a Composite widget comprising of a QToolButton,QLabel and QCalendarWidget.

Now create a QtGui Application by selecting File-New-Qt Widget Project-Qt Gui Application from the Qt Creator.

The above procedure will create a project consisting of four files with the user defined class name:

  • main.cpp
  • calendardialog.h
  • calendardialog.cpp
  • calendardialog.ui

Creating a Custom Calendar by Editing the files

Name the class with the Choice Name.Current class Name is Calendar.

open the calendardialog.ui, from the Designer view, Drag and drop the following controls.

Four tool buttons. yearBackButton, yearFrontButton, MonthBackButton, MonthFrontButton Two labels, Titlelabel, DateLabel, Three horizontal spacers, One Button OkButton, and a calendar widget. calendarWidget

Now open the calendardialog.h and edit,

add the required slots to the calendardialog.h file.

calendardialog.h

#ifndef CALENDARDIALOG_H
#define CALENDARDIALOG_H

#include "ui_calendardialog.h"

class Calendar : public QDialog
{
 Q_OBJECT

public:
 explicit Calendar(QWidget *parent = 0);
 private:
 Ui::calendarDialog ui;

private:
 QDate getdate();
 QDate currentDate() const;
 void setCurrentDate(const QDate &amp;d);
 void on_calendar_clicked(const QDate &amp;date);

private slots:

void on_MonthFrontButton_clicked();
 void on_MonthBackButton_clicked();

void on_yearBackButton_clicked();
 void on_yearFrontButton_clicked();

void on_OkButton_clicked();
};

#endif // CALENDARDIALOG_H

edit the corresponding calendardialog.cpp, connect and implement the slots like below.

calendardialog.cpp

#include <QCalendarWidget>
#include <QDate>
#include <QApplication>
#include <QMessageBox>

#include "calendardialog.h"

Calendar::Calendar(QWidget *parent) :
 QDialog(parent)
{
 ui.setupUi(this);
 setWindowFlags(Qt::FramelessWindowHint);
 ui.calendarWidget->setNavigationBarVisible(false);
 ui.calendarWidget->setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames);
 ui.calendarWidget->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);

QObject::connect(ui.yearBackButton,SIGNAL (clicked()),this,SLOT (on_yearBackButton_clicked()));
 QObject::connect(ui.yearFrontButton,SIGNAL (clicked()),this,SLOT (on_yearFrontButton_clicked()));
 QObject::connect(ui.MonthBackButton,SIGNAL (clicked()),this,SLOT (on_MonthBackButton_clicked()));
 QObject::connect(ui.MonthFrontButton,SIGNAL (clicked()),this,SLOT (on_MonthFrontButton_clicked()));

QObject::connect(ui.OkButton,SIGNAL (clicked()),this,SLOT (on_OkButton_clicked()));

}

QDate Calendar::currentDate() const {
 return ui.calendarWidget->selectedDate();
}
void Calendar::setCurrentDate(const QDate &amp;d) {
 QDate date = d;
 if (!date.isValid())
 date = QDate::currentDate();
 ui.calendarWidget->setSelectedDate(date);
 ui.calendarWidget->showSelectedDate();
 on_calendar_clicked(date);
}

void Calendar::on_calendar_clicked(const QDate &amp;date) {
 ui.DateLabel->setText(date.toString("dd MMMM yyyy"));
}

QDate Calendar::getdate ()
{
 QMessageBox showDateMessage(QMessageBox::Warning,
 ui.calendarWidget->selectedDate().toString("dd MMMM yyyy"),"",QMessageBox::Ok,0);
 ui.DateLabel->setText(ui.calendarWidget->selectedDate().toString("dd MMMM yyyy"));
 showDateMessage.setText("Date Selected is "+ui.calendarWidget->selectedDate().toString("dd MMMM yyyy"));
 int ret = showDateMessage.exec();
 if(ret == QMessageBox::Ok)
 {

accept();
 return ui.calendarWidget->selectedDate();

}
 else
 {
 return QDate();
 }

}
void Calendar::on_yearBackButton_clicked() {
 QDate date = currentDate();

date = date.addYears(1);
 setCurrentDate(date);
}

void Calendar::on_yearFrontButton_clicked() {
 QDate date = currentDate();
 date = date.addYears(+1);
 setCurrentDate(date);
}

void Calendar::on_MonthFrontButton_clicked() {
 QDate date = currentDate();
 date = date.addMonths(+1);
 setCurrentDate(date);
}

void Calendar::on_MonthBackButton_clicked()
{
 QDate date = currentDate();
 date = date.addMonths(1);
 setCurrentDate(date);
}

void Calendar::on_OkButton_clicked()
{
 getdate();
}

The UI file looks like this in the editable xml file format.

calendardialog.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>calendarDialog</class>
 <widget class="QDialog" name="calendarDialog">
 <property name="geometry">
 <rect>
 <x>0</x>
 <y>0</y>
 <width>400</width>
 <height>300</height>
 </rect>
 </property>
 <property name="windowTitle">
 <string>calendarDialog</string>
 </property>
 <layout class="QGridLayout" name="gridLayout">
 <item row="0" column="0" colspan="2">
 <widget class="QLabel" name="Titlelabel">
 <property name="text">
 <string>Date Selector</string>
 </property>
 <property name="alignment">
 <set>Qt::AlignCenter</set>
 </property>
 </widget>
 </item>
 <item row="1" column="0" colspan="2">
 <layout class="QHBoxLayout" name="horizontalLayout">
 <item>
 <widget class="QToolButton" name="yearBackButton">
 <property name="minimumSize">
 <size>
 <width>30</width>
 <height>0</height>
 </size>
 </property>
 <property name="text">
 <string><<</string>
 </property>
 </widget>
 </item>
 <item>
 <spacer name="horizontalSpacer">
 <property name="orientation">
 <enum>Qt::Horizontal</enum>
 </property>
 <property name="sizeType">
 <enum>QSizePolicy::Fixed</enum>
 </property>
 <property name="sizeHint" stdset="0">
 <size>
 <width>20</width>
 <height>20</height>
 </size>
 </property>
 </spacer>
 </item>
 <item>
 <widget class="QToolButton" name="MonthBackButton">
 <property name="text">
 <string><</string>
 </property>
 </widget>
 </item>
 <item>
 <widget class="QLabel" name="DateLabel">
 <property name="text">
 <string/>
 </property>
 <property name="alignment">
 <set>Qt::AlignCenter</set>
 </property>
 </widget>
 </item>
 <item>
 <widget class="QToolButton" name="MonthFrontButton">
 <property name="text">
 <string>></string>
 </property>
 </widget>
 </item>
 <item>
 <spacer name="horizontalSpacer_2">
 <property name="orientation">
 <enum>Qt::Horizontal</enum>
 </property>
 <property name="sizeType">
 <enum>QSizePolicy::Fixed</enum>
 </property>
 <property name="sizeHint" stdset="0">
 <size>
 <width>20</width>
 <height>20</height>
 </size>
 </property>
 </spacer>
 </item>
 <item>
 <widget class="QToolButton" name="yearFrontButton">
 <property name="minimumSize">
 <size>
 <width>30</width>
 <height>0</height>
 </size>
 </property>
 <property name="text">
 <string>>></string>
 </property>
 </widget>
 </item>
 </layout>
 </item>
 <item row="2" column="0" colspan="2">
 <widget class="QCalendarWidget" name="calendarWidget"/>
 </item>
 <item row="3" column="0">
 <spacer name="horizontalSpacer_3">
 <property name="orientation">
 <enum>Qt::Horizontal</enum>
 </property>
 <property name="sizeHint" stdset="0">
 <size>
 <width>298</width>
 <height>20</height>
 </size>
 </property>
 </spacer>
 </item>
 <item row="3" column="1">
 <widget class="QPushButton" name="OkButton">
 <property name="maximumSize">
 <size>
 <width>150</width>
 <height>60</height>
 </size>
 </property>
 <property name="text">
 <string>Ok</string>
 </property>
 </widget>
 </item>
 </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

Finally Create an instance of the Calendar class in main.cpp and call show() method before entering the event loop by calling a.exec()

main.cpp

#include <QtGui/QApplication>
#include "calendardialog.h"

int main(int argc, char '''argv[])
{
 QApplication a(argc, argv);
 Calendar w;
 w.show();

 return a.exec();
}

With the above example files one can easily create a custom calendar widget.

h2. See also

"How to use signals and slots":http://developer.qt.nokia.com/wiki/How-to-use-signals-and-slots