Click here to Skip to main content
15,885,953 members
Articles / Desktop Programming / QT

Calendar - calendar with notes using the Qt framework

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
12 May 2017CPOL2 min read 14.2K   1.1K   4  
A simple calendar with notes support

Image 1Image 2

Introduction

This program is written using the Qt framework - modern cross-platform development tools. Qt allows you to run the software written with it on most modern operating systems by simply compiling the program for each OS without changing the source code. Includes all the major classes that you might need when developing application software, from GUI elements to classes for working with the network, databases and XML. Qt is completely object-oriented, easily extensible and supports the technology of component programming.

Background

In the process of working, I needed a widget displaying the calendar for the whole year. The standard Qt widget QCalendarWidget is designed to display only one month. I tried to write my widget using QAbstractTableModel - abstract model, a subclass of which you can create to implement your own table model. My widget displays the calendar both for the year and for the month. Allows you to select the day of the beginning of the week, add and delete notes for the day of the week.

Using the code

In the process of work I used Qt version 5.8.0 and Microsoft Visual Studio 2013. Note: program compiled with version Qt 5.8.0 will not work in Windows XP due to use of the function - GetUserPreferredUILanguages. If the computer was not installed Visual Studio 2013 to run the program you need Microsoft Visual C++ 2013 Redistributable Package.

Points of Interest

CalendarGadget - class displaying the calendar for the month, сontains QTableView with model MyCalendarModel.

For storage of information in the model MyCalendarModel used the following structure:

C++
struct Record
    {
        int day; 
        int month;
        int year;
        bool notes_present;
    };

The data in this structure is written by the function CalendarModel::updateModel(). Correct update of the data did not occur without beginResetModel() and endResetModel(). To return data stored by the model, use the function CalendarModel::data(const QModelIndex &index, int role). Also, using this function with the Qt :: ToolTipRole, a list of notes for the day under the mouse cursor is displayed.

To store objects of the CalendarGadget type, a very convenient QVector class is used which refers to container classes and provides access to items by index, as well as a number of additional methods for convenience.

C++
QVector <CalendarGadget*> Calendars;

for (int i = 1; i <= 12; i ++)
    {
        CalendarGadget *gad = new CalendarGadget;
        Calendars.append(gad);
        gad->setMonth(i);

        connect(year, SIGNAL(valueChanged(int)), gad, SLOT(gotoYear(int)));
        connect(firstDay, SIGNAL(currentIndexChanged(int)), gad, SLOT(firstDayChanged(int)));
    }

To store the notes I used QSQLite will be a Qt database driver for the SQLite Database. The calendar notes are stored in the calendar.db file in the folder where myQtCalendar.exe is started. In order that the framework could find the driver in the folder of my program I used the following code:

C++
QString plugins_patch = QCoreApplication::applicationDirPath();
QStringList paths = QCoreApplication::libraryPaths();
paths.append(plugins_patch + "/plugins/");

To execute SQL commands, after the connection is established, the QSqlQuery class is used. Each request is analyzed for errors with QSqlQuery::lastError(). To directly edit notes, delete and add, I use my addNote() class inherited from QDialog.

History

  • Version 0.1
    • Initial version
  • Version 1.0
    • Adding work with notes. Error correction.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --