Hello everyone, I am Lao Wu, a hard-working embedded bottom working people.

I double 叒 yi to share Qt learning experience.

The best way to learn Qt is to read the official manuals and examples,

Today we will share an official Qt example: Notepad.

Notepad is a simple text editor:

Click for a larger version

It supports new, open, save, copy, paste, cut, undo, redo and other functions of the mini-text editor, similar to Gedit.

Source code:

` notepad / ` ` ├ ─ ─ images ` ` │ ├ ─ ─ bold. PNG ` ` │ ├ ─ ─ copy. PNG ` ` │ ├ ─ ─ the create. PNG ` ` │ ├ ─ ─ the cut. The PNG ` ` │ ├ ─ ─ edit_redo. PNG ` ` │ ├ ─ ─ edit_undo. PNG ` ` │ ├ ─ ─ the exit. The PNG ` ` │ ├ ─ ─ the font. The PNG ` ` │ ├ ─ ─ the info. The PNG ` ` │ ├ ─ ─ italic. PNG ` ` │ ├ ─ ─ new. PNG ` ` │ ├ ─ ─ Open. PNG ` ` │ ├ ─ ─ paste. The PNG ` ` │ ├ ─ ─ pencil. PNG ` ` │ ├ ─ ─ print. The PNG ` ` │ ├ ─ ─ save_as. PNG ` ` │ ├ ─ ─ the save. PNG ` ` │ └ ─ ─ Underline the PNG ` ` ├ ─ ─ main. CPP ` ` ├ ─ ─ notepad. CPP ` ` ├ ─ ─ notepad. H ` ` ├ ─ ─ notepad. Pro ` ` ├ ─ ─ notepad. The QRC ` ` └ ─ ─ notepad. UI `Copy the code

Here’s a quick tutorial on how to implement the gadget, Let’s Go.

Body contents:

Create a new Qt Widgets Application. Use Qt Designer to design interface. 3. Implement New/Open/Save file functions. 5. You Can Do Anything because you Don't have timeCopy the code

1. Create the Qt Widgets Application

In Qt Creator, click,

File > New File or Project > Applications > Qt Widgets Application > Choose:

Click for a larger version

Use QMainWindow as the base class and subclass Notepad:

You will get the following files:

` notepad / ` ` ├ ─ ─ main. CPP ` ` ├ ─ ─ notepad. CPP ` ` ├ ─ ─ notepad. H ` ` ├ ─ ─ notepad. Pro ` ` └ ─ ─ notepad. The UI `Copy the code

Notepad. CPP and notepad. H are the source code for the classes corresponding to the Notepad Widget.

Notepad. UI is the UI file corresponding to the Notepad Widget.

Operation effect:

At this point, there is only the same interface as QMainWindow and no editor related functionality.

2. Use Qt Designer to design the interface

The Notepad interface is divided into four parts:

Click for a larger version

  1. Menu bar, with QMenuBar implementation;

  2. Toolbar, implemented with QToolBar;

  3. Text edit box, with QTextEdit implementation;

  4. Status bar, with QstatusBar implementation;

This article won’t cover all of Notepad’s features. Instead, here are three examples:

  • Create a file

  • Open the file

  • Save the file

As long as the implementation logic of these three features is clear, there is no problem implementing the entire Notepad.

In Qt Designer:

  • Modify menu bar, add File menu item

  • Modify the toolbar by adding New, Open and Save actions

  • Add edit box

Operation effect:

Buttons in the menu bar and toolbar do not have any function.

3. Implement the function of New/Open/Save files

Use the concept of Action in Qt to simplify menu bar and toolbar programming,

Each menu item has its own Action. Developers can customize the Action in the Action Editor in Qt Designer.

When the user clicks on a menu item, the QAction::triggered signal is emitted and the corresponding slot function needs to be set to achieve the corresponding function.

Binding signal and slot:

CPP ' 'notepad :: notepad (QWidget *parent)' ': QMainWindow(parent)' ', UI (new UI :: notepad) ' '{'... ` `connect(ui->actionNew, &QAction::triggered, this, &Notepad::newDocument); ` `connect(ui->actionOpen, &QAction::triggered, this, &Notepad::open); ` `connect(ui->actionSave, &QAction::triggered, this, &Notepad::save); ` ` `}Copy the code

Realize the function of creating new files

H ' 'class notepad: public QMainWindow' '{'... ` `private slots:` `void newDocument(); ` `void open(); ` `void save(); ` `private:` `Ui::Notepad *ui; ` `QString currentFile; ` `}; `Copy the code

CurrentFile is used to save the path name of the currentFile.

`void Notepad::newDocument()` `{` `currentFile.clear(); ` `ui->textEdit->setText(QString()); ` `setWindowTitle("new File *"); ` ` `}Copy the code

The process of creating a new file is as simple as emptying the text box.

Operation effect:

Realize the function of saving files


CPP ' 'void notepad ::save() {'' QString fileName; ` ` / / if the previous did not Save, is to ask the user to Save where ` ` if (currentFile. IsEmpty ()) {` ` fileName = QFileDialog: : getSaveFileName (this, "Save"); ` `currentFile = fileName; ` `} else {` `fileName = currentFile; ' '} ' '// write the text in the text box to the file' 'QFile file(fileName); ` ` [...]. ` `setWindowTitle(fileName); ` `QTextStream out(&file); ` `QString text = ui->textEdit->toPlainText(); ` `out << text; ` `file.close(); ` ` `}Copy the code

Qt provides the QFile class for file operations.

The QFile class itself has an interface for reading and writing files, but it is usually more convenient to use QTextStream or QDataStream to read and write files.

In addition:

  • For temporary files, you can use QTemporaryFile,

  • To get file information, use QFileInfo,

  • You can use QDir to process directories,

  • You can use QFileSystemWatcher to monitor file and directory changes.

Operation effect:

Realize the function of opening files


` / / file: Notepad. CPP ` ` void notepad: : open () ` ` {` ` / / the user to choose the file to open ` ` QString fileName = QFileDialog: : getOpenFileName (this, "Open the file"); ` `QFile file(fileName); ` ` [...]. ` `currentFile = fileName; ` `setWindowTitle(fileName); QTextStream in(&file); QTextStream in(&file); ` `QString text = in.readAll(); ` `ui->textEdit->setText(text); ` `file.close(); ` ` `}Copy the code

As you can see from the above code, text flow in Qt is very simple.

Operation effect:

This completes the core functionality of the Notepad example. Read the full Qt Example code for yourself.

** file IO is a very important part of the programming world, ** in many examples of Qt to use this piece of knowledge, here we first have a preliminary understanding of QTextStream, subsequent need to practice Qt IO operations in more examples.

Relevant reference

Qt Official Documentation:

  • Creating Main Windows in Qt Designer

  • File and Datastream Functions

C++ GUI Qt4 programming (second edition)

Introduction to Qt5 Programming (Second Edition)

Qt Creator Quick Start

Think about technology, but also think about life

To learn technology, but also to learn how to live.

Good book recommendations:

I Can Do Anything because I don’t have the time

Author: Yoshida Hoba

Doctor of obstetrics and gynecology, Nagoya University Research Institute, 2004. While she was busy with work and family, she felt that she had to improve herself to make a difference and decided to study again. From applying for admission to Harvard, preparing for the exam to being admitted, it only takes half a year! In 2008, she and her husband moved to Boston with their three daughters, ages 3, 1 and 1 month, to earn a Harvard degree in two years.

Douban score: 8.0, 790 comments

Click for a larger version

What can be gained?

  • A different sense of time;

  • Practical time management methods;

  • Positive psychological construction;

  • Acceptance of disorder;

  • Accept that things are only partially done;

  • Ask others to help you with non-core tasks.

You and I each have an apple. If we exchange apples, we still have only one apple. But when you and I each have an idea, and we exchange ideas, we both have two ideas.

Feel the article is valuable to you, might as well read + share.

Recommended reading:

Album | Linux driver development

Album | Linux system programming

Album | a little C every day

Album | Qt entry