QTcreator develops a diary application (novice quick start GIF display)

Write in front

Personally, I am a more sentimental person. Even the long-used app on the mobile phone will have a sense of dependence, and the "Wuji" app has entered my life long ago with its simplicity and art. During this period of time learning QT development, it can be regarded as a process of consolidating C ++. I want to write a low-profile "Wuji" on the win side by myself . The whole project is still under development, but the general framework is there. Now, the main functions are basically formed, some personalized functions will be added later, and this blog will continue to be updated.

Development environment: win10, QT creator, MINGW
Development language: C++11 or above,
basic development knowledge: object-oriented

Project github:https://github.com/ZYunfeii/QT
environment variables need to be configured by themselves

Login interface

This block currently has three built-in accounts, one test account (for debugging), and two user accounts. It can be regarded as a more traditional login interface. Just enter the account and password and click to log in. The account registration function has not yet been developed (because I use it myself).
Insert picture description here
The implementation method is quite simple. Two QStringLists correspond to the account password, the button signal slot link, and the user name index is retrieved.

Main interface

This part is the interface that the user enters after successfully logging in. The overall painting style is not beautiful enough, and the UI designer needs to beautify hhh.
Insert picture description here

This part is mainly to display the diary that has been written and delete the diary (there is a small × on the right). The diary includes content, time, font and font size at the time, etc. Of course, there is also the user's avatar... All data is managed and stored through the database SQLite , which will be introduced later. The advantage of using a database is that all the data is available for the next login, and the database is also very convenient to operate with C ++.

New diary interface

Click the New button in the upper right corner of the main interface to create a new diary:
Insert picture description here
This editing page currently supports the adjustment of font and font size. After writing down today's happiness or troubles, click Save in the upper right corner and record it!
Insert picture description here
The diary you just wrote is automatically put on top, and the font size is the same as the edited one.

If you are disappointed with your past self, then please delete the silly things you wrote down those times (Click the red × on the right),The database will also delete this recordAnd, you have no way to restore it anymore. After all, the past will never come again, and the clock will never stop.

Database SQLite

QT comes with its own database SQLite. When using the database, don't forget to add this sentence to the pro file:

QT += sql

Create multiple tables in the database:

        //创建n个数据库(n = usernameList.length())
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",QString("myDiary%1").arg(i));
        db.setDatabaseName(QString(".//qtDb%1.db").arg(i));
        if( !db.open())  qDebug() << "无法建立数据库连接";       //这个db.open相当关键啊!没这一步后面写数据可失败。
        else qDebug()<<"数据库连接成功";

        QSqlQuery query(db);
        query.exec("create table person (time varchar, diary varchar,family varchar,pointSize varchar)");
        db.close();

Don't forget: #include <QSqlQuery> #include <QSqlDatabase>
In addition, during the first operation, don't forget that the db.open()
database contains four pieces of information: time, diary content, font, font size, which may increase in the future.

The database of the corresponding user is linked in the main interface, and many functions can be completed by using the database. For example, the function of counting the number of diaries can be realized as follows:

    //初始化日记数量并显示在顶部
    query.first();
    query.previous();
    if(query.last())
    {
    
    
        numberOfRows =  query.at() + 1;
    }
    ui->diaryNumLabel->setText(QString("您已累计写下了%1篇日记").arg(numberOfRows));

Query is the database "pointer" you get. First move the query to the previous virtual location of the first data, and then query.last goes down until the query points to the last number, and the index plus 1 is the total database capacity.
Let me post a few blog posts about database knowledge:
QT-QSqlQuery class operation SQLite database (create, query, delete, modify) Detailed explanation of
Qt database: (3) Use QSqlQuery class to execute SQL statements (1)
Qt's own database QSQLITE (a very specific example)
Qt learning road eight-use qt to operate the database You
can basically get started after reading it, don't ask, just try!

Of course there are still many pits in the database. For example, when I use the date string to delete, the database compares the string with single quotes. At this time, do this:

QString deleteString = QString("%1%2%3").arg("'").arg(time).arg("'");                 
bool flag = query.exec(QString("DELETE FROM person WHERE time = %1").arg(deleteString));

Hey, little trick ...

Combine your own controls

Insert picture description here
A control like this is an encapsulated control made of multiple controls. The specific method will not be repeated ( promoted to operation ). The opposite object is the soul.

scrollArea和Layout

These two spaces constitute the main body of the main interface. The scrollArea is like a window covered on the tablecloth. Understanding this sentence can understand the control. The layout determines the arrangement rules of the things on the tablecloth. Here is a blog post:
How to use QScrollArea of ​​QT's automatic scrolling area, detailed graphic and text

Project to be continued

The white bar under the main interface is for future expansion. Language learning is still effective in development.
The project has been uploaded to githubhttps://github.com/ZYunfeii/QT

November 8:
Insert picture description here

November 9th:
Added the diary record function, supports double-clicking to pop up the diary details, and the database is also connected:
Insert picture description here

November 16th:
Some optimizations were made to the login interface: default login name, and use the enter key to quickly log in. I also changed the icon of the exe executable program. I have started to use it now, and it feels good:
Insert picture description here
November 17th:
Rewrite the map associative container for the login interface user name and user password, and add multiple animation effects to the user interface :
Insert picture description here

November 22: I
suddenly came up with an idea today to make a function to the login interface, when the account and password are entered correctly, the avatar will be loaded (a little bit different from qq)
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43145941/article/details/109559455