Calendar – calendar with notes using the Qt framework

0
39

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:

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.

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:

 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.

LEAVE A REPLY