当前位置:网站首页>Qt5 development from introduction to mastery -- the first overview
Qt5 development from introduction to mastery -- the first overview
2022-07-28 13:55:00 【Joey Boye o (* ^ ^ ^ *) o】
Welcome comments from friends , Learn from each other 、 Mutual relation must be returned 、 Online all day
Blogger 🧑🧑 Communicate in the spirit of open source Qt Development experience 、 The continuation chapter will be continuously updated , Contribute bloggers' own open source spirit to the community
List of articles
One 、 What is? Qt
`
Qt It's a cross platform C++ GUI application framework . It provides application developers with the ability to build art
All the functions required by the graphical user interface . It's completely object-oriented , It's easy to expand , And allow real component programming .
1.1、Qt The development of
Qt Is in 1991 Developed by Qiqu technology company in , On 1996 In, he entered the business field , Become thousands around the world
The foundation of a successful application . It's also popular right now Linux Desktop Environment KDE The basis of , KDE yes Linux issue
The main standard component of version . 2008 year , Qiqu technology company was acquired by Nokia , Qt Become a programming company under Nokia
Language tools . from 2009 year 5 Released on Qt4.5 From Edition , Nokia announces Qt The source code library is open to the public , Qt open
The sender can pass as Qt And its related project contribution code 、 translate 、 Examples and more , Help guide and shape Qt
The future development of . 2011 year , Digia company ( A family in Finland IT Services ) Acquired from Nokia Qt Commercial copyright of .
2012 year 8 month 9 Japan , As part of the non core divestiture plan , Nokia announced that it would Qt The software business is officially SOLD
to Digia company . 2013 year 7 month 3 Japan ,Digia company Qt The development team announced on its official blog Qt 5.1 The official version of
cloth ; Same year 12 month 11 Japan , Again Qt 5.2 Official version . 2014 year 4 month , Cross platform integrated development environment Qt Creator 3.1.0
Official release ; Same year 5 month 20 Japan , Supporting release Qt 5.3 Official version . thus , Qt To achieve for iOS 、 Android 、 WP
And other platforms . The Qt Company After the establishment of the company , Qt The upgrade of version begins to accelerate , Rolled out
Qt 5.4/5.5/5.6/5.7 edition , And provide free download on the official website . 2017 year 1 month 23 Japan , What people expect Qt 5.8 The official version is like
Issue , It embodies the previous editions Qt Product advantages , Further expanded Qt Framework , Improved speed and performance .
Especially for Qt Of Qt Quick Controls 2 Library and 3D Support has been significantly optimized and upgraded . 2018 year 5 month , Qt Hair
The latest Qt 5.11 edition .
1.2、Qt Version Description
Qt Distributed in different versions , Divided into commercial version and open source version . Qt The commercial edition provides a development environment for commercial software , they
Provide traditional commercial software distributions , And provide free upgrades and technical support services within the validity of the agreement . and Qt Open source Edition
It is open source software designed for free development , It provides the same functionality as the commercial version , stay GNU General public
Under license , It's free .
1.3、Qt5 Introduction to the development environment
stay Qt In the process of program development , In addition to realizing software development functions through handwritten code , You can also use Qt Of GUI
Interface Designer (Qt Designer) Draw and layout the interface . The tool provides Qt Basic paintable widgets for ,
Such as QWidget、 QLabel 、 QPushButton and QVBoxLayout etc. . Drag these windows directly with the mouse in the designer
parts , Can be efficient 、 Quickly achieve GUI Interface design , The interface is intuitive , What you see is what you get . Qt The main interface of the designer is shown in the following figure 
Get into Qt After the main interface of the designer , See the form part of the design area ( As shown in the figure below ) Is the top-level widget to be designed ( The top-level widget is the carrier of other sub widgets ).
stay Qt The left side of the designer main interface ” Component box ” Column lists frequently used Qt Standard widgets , It can be straight
Then drag the corresponding widget icon to the interface of the top-level widget . meanwhile , You can also combine the designed widgets
( Through the layout manager Qt Layout and combination of standard widgets ) Or place other widgets Qt Container class ( see
” Component box ” Column “Containers” Group ) Drag directly to “ Component box ” In the bar , Qt The designer will be automatically in ” Components
box ” Column generates " Scratchpad" Group , And generate new custom widgets . thereafter , It can be like using Qt Provide
Use the newly created widget just like the standard widget .
Choose Qt Designer main interface “ Control " -> " View ” All options in , stay Qt You can see
To some editing tool sub windows provided by the designer ( Pictured 1.13 Shown ).
• Object viewer (Object Inspector): Lists all the widgets in the main interface , And the parent of each widget
Sub relationships and inclusive relationships .
• Property editor (Property Editor): Lists the editable properties of the widget .
• Action Editor (Action Editor): Lists... Designed for widgets QAction action , adopt “ add to ” or
“ Delete ” Button can create a new named QAction Action or delete the specified QAction action .
• Signal and slot editor (Signals & Slots Editor): Listed in Qt Associated signals and slots in the designer , adopt
Double click the object or signal in the column / Slot , You can select objects and signals / Slot selection .
Besides , adopt Qt Designer's “ edit ” menu , Can open Qt Four types of designer main interface GUI Window components
Editing mode .
• Control edit mode (Edit Widgets): Can be in Qt Add GUI Widget and modify it
Their properties and appearance .
• The signal / Slot edit mode (Edit Signals/Slots): Can be in Qt Associated on the widget in the main interface of the designer Qt
Already defined signals and slots .
• Partner editing mode (Edit Buddies): Can be in Qt On the widget in the main interface of the designer QLabel mark
Sign partnerships with other widgets .
•Tab Sequential editing mode (Edit Tab Order): Can be in Qt Settings on widgets in the main interface of designer Tab
Key focus order on the widget .
Two 、 Simple project design
2.1、 Designer Qt ~DesJgner Realization
【 example 】( Simple ) Adopt designer Qt Designer Realize the calculation of circular area , Complete the functions shown in the figure below
First, design the interface , Then write the corresponding code to calculate the circular area .
2.1.1 Interface design
Steps are as follows .
(1) Click Run Qt Creator, Get into Qt Creator Initial interface . Click
new Project Button , Or choose “ file " – “ Create a new file or project ..” command , Create a new project , Out
present “ New projects ” window ( Here's the picture 2.1.1 Shown ).
(2) Click to select an item “Application” -> “Qt Widgets Application” Options , single click “Choose …”
Button .
Programmers need to create what kind of project to choose the corresponding project options . for example , “Qt Console Application”
The option is to create a project based on the thousand console . Here, because you need to build a desktop application , So choose "Qt Widgets
Application”( Here's the picture 2.1.2 Shown ) .
(3) Choose the path to save the project and define the name of your project . Be careful , The path to save the project cannot contain Chinese .
There is no case requirement for project naming , Name it according to personal habits . Here the project is named Dialog, Save path as
D:\Qt_1, ( Here's the picture 2.1.3 Shown ). single click “ next step ” Button .
(4) eject “ 粔 t Selection” ( Choose build suite ) Interface , By default, the system has specified C++ Compiler and debugger ,
( Here's the picture 2.1.4 Shown ), Directly click “ next step ” Button .
(5) According to actual needs , Select a “ Base class ”. Choose here QDialog Dialog class as base class , At this time “ Class name “
“ The header file ”“ Source file ”“ Interface file ” The default file name appears . Be careful , These file names can be used according to specific needs
Make corresponding modifications . Selected by default “ Create interface “ Check box ( Here's the picture 2.1.5 Shown ), Indicates that the interface designer is required
To design the interface , Otherwise, you need to use code to complete the design of the interface .
(6) When the selection is complete, click “ next step ” Button , The corresponding files are automatically loaded into the project file list , Here's the picture 2.1.5 Shown ).
chart 2.1.1
chart 2.1.2
chart 2.1.3
chart 2.1.4
chart 2.1.5
chart 2.1.6
2.2.1 Write the corresponding code for calculating the circular area
First of all, let's have a brief understanding of Qt Programming environment . Find the automatically added... In the file list main.cpp file , Pictured 1.21 Shown .
Each project has an entry function to execute , In this document main() Function is the entry of this project .
Here is a detailed introduction main() Function related content :
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
among ,
( a) #include “dialog.h”: Contains the functions to be completed in the program Dialog The definition of a class , stay Dialog Class
Complete the required functions . Be careful , The header file containing the class must be referenced to which class to use . for example , If you use a
Button class , You must add a line of code here #include < QPushButton >, This indicates the inclusion of buttons
(QPushButton) The definition of a class .
( b) #include < QApplication >: Application The definition of a class . In each use Qt Must be in a graphical application
One must be used QApplication object . QApplication Manages a wide range of resources for a wide variety of graphical applications 、 The base
This setting 、 Control flow and event handling .
( c) int main(int argc, char *argv[ ]): Entry to the application , Almost in all use Qt Under the circumstances , main()
The function only needs to transfer control to Qt Perform initialization before the Library , then Qt The library informs the program of the user's behavior through events .
all Qt There must be only one in the program main() function . main() Function has two arguments , namely argc and argv ,argc
Is the number of command line variables , argv Is an array of command line variables .
( d) QApplication a(argc, argv): a It's the of this program QApplication object . In any Qt The window system of
Components must be created before being used QApplication object . It is created here and handles these command line variables . all
By Qt The recognized command line parameters will be from argv Is removed ( also argc It also reduces ).
( e) w.show( ): When creating a widget , By default, it is invisible , Must call show( ) Function makes it
Become visible .
( f) return a.exec( ): The program enters the message loop , Wait for possible input to respond . Here is the main() Function will
Control is transferred to Qt, Qt Complete the event handling work , When the application exits , exec( ) The value of the function will return .
stay exec() Function , Qt Receive and process user and system events and pass them to the appropriate widgets .
2.2.2 Write the corresponding code to calculate the circular area
Now? , There are two ways to complete the function of calculating the circular area : One is to complete by triggering the button event ( The way 1); Two is
Complete by triggering the input edit box event ( The way 2) .
The way 1: stay “lineEdit” Enter the radius value in the text box , Then click the button , It's in lineEdit_2
The corresponding circle area is displayed in .
The steps of writing code are as follows .
(1) stay “ Calculation ” Press the right mouse button , Select... From the pop-up drop-down menu “ Go to slot …” command , stay “ go to
Slot ” Select... In the dialog box QAbstractButton Of “clicked( )” The signal , single click “OK” Button , Pictured 2.2.1 、 chart 2.2.2 、 chart 2.2.3 、 chart 2.2.4( object ) Shown .
chart 2.2.1
open ui After the interface , Pull the corresponding control into the edit box , Here's the picture 2.2 .
chart 2.2

chart 2.2.2
chart 2.2.3
chart 2.2.4( object )
(2) Get into dialog.cpp The slot function of the button click event in the file on_pushButton_clicked(). The signal is connected to the slot
Specifically, the following chapters will explain . Add the following code to this function :
void Dialog::on_pushButton_clicked()
{
bool ok;
QString tempStr;
QString valueStr = ui->lineEdit->text();
int valueInt = valueStr.toInt(&ok);
double area = valueInt*valueInt*PI;
ui->lineEdit_2->setText(tempStr.setNum(area));
}
(3) stay dialog.cpp Add the following statement at the beginning of the file :
const static double PI = 3.1416;
Define global variables PI .
Method 2: stay “Line Edit” Enter the radius value in the text box , There is no need to click the button to trigger the click event , It's right there
lineEdit_2 The circle area is shown in .
The steps of writing code are as follows .
(1) stay “Line Edit” Press the right mouse button on the text box , Select... From the pop-up drop-down menu “ Go to slot …” command , stay
“ Go to slot ” Select... In the dialog box QLineEdit Of “textChanged(QString)” The signal , Pictured 2.2.5 Shown .
( 2) single click “OK” Button , Get into dialog.cpp The text edit box in the file changes the slot function of the value content event
on_lineEdit_textChanged(const QString &arg1). Add the following code to this function
void Dialog::on_lineEdit_textChanged(const QString &arg1)
{
bool ok;
QString tempStr;
QString valueStr = ui->lineEdit->text();
int valueInt = valueStr.toInt(&ok);
double area = valueInt*valueInt*PI;
ui->lineEdit_2->setText(tempStr.setNum(area));
}
Run this program , stay “Line Edit” Enter the radius value in the text box , Directly in lineEdit_2 The area of the circle is shown in
value , Complete the function of calculating the circular area .
3、 ... and 、dialog.cpp Source code is as follows
#include "dialog.h"
#include "ui_dialog.h"
const static double PI = 3.1416;
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
bool ok;
QString tempStr;
QString valueStr = ui->lineEdit->text();
int valueInt = valueStr.toInt(&ok);
double area = valueInt*valueInt*PI;
ui->lineEdit_2->setText(tempStr.setNum(area));
}
void Dialog::on_lineEdit_textChanged(const QString &arg1)
{
bool ok;
QString tempStr;
QString valueStr = ui->lineEdit->text();
int valueInt = valueStr.toInt(&ok);
double area = valueInt*valueInt*PI;
ui->lineEdit_2->setText(tempStr.setNum(area));
}
Four 、 design sketch as follows

After compiling

Input 2
边栏推荐
- 在 Kubernetes 中部署应用交付服务(第 1 部分)
- JWT 登录认证 + Token 自动续期方案,写得太好了!
- 安全保障基于软件全生命周期-Istio的授权机制
- Generation of tables and contingency tables (cross tables) of R language factor data: use the summary function to analyze the list, view the chi square test results, and judge whether the two factor v
- Holes in [apue] files
- C language: merge sort
- 酷炫操作预热!代码实现小星球特效
- 数据库系统原理与应用教程(058)—— MySQL 练习题(二):单选题
- To build agile teams, these methods are indispensable
- 30天刷题计划(四)
猜你喜欢

Socket类关于TCP字符流编程的理解学习

strcmp、strstr、memcpy、memmove的实现

不用Swagger,那我用啥?

Org.apache.ibatis.exceptions.toomanyresultsexception

安全保障基于软件全生命周期-Istio的授权机制

Algorithm --- different paths (kotlin)

Can second uncle cure young people's spiritual internal friction?

多线程与高并发(三)—— 源码解析 AQS 原理

使用 IPtables 进行 DDoS 保护

Beyond istio OSS -- current situation and future of istio Service Grid
随机推荐
POJ3259虫洞题解
比XShell更好用、更现代的终端工具!
数据库系统原理与应用教程(062)—— MySQL 练习题:操作题 32-38(六)
R language uses LM function to build linear regression model and subset function to specify subset of data set to build regression model (use floor function and length function to select the former pa
【飞控开发基础教程7】疯壳·开源编队无人机-SPI(气压计数据获取)
Cool operation preheating! Code to achieve small planet effect
SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误的试读版
使用 IPtables 进行 DDoS 保护
微信小程序中自定义模板
【安全】 阅读 RFC6749 及理解 Oauth2.0 下的授权码模式
What is the reason why the words behind word disappear when typing? How to solve it?
Product Manager: job responsibility table
R语言使用lm函数构建线性回归模型、使用subset函数指定对于数据集的子集构建回归模型(使用floor函数和length函数选择数据前部分构建回归模型)
30天刷题计划(三)
拒绝服务 DDoS 攻击
Li Kou sword finger offer 51. reverse order pairs in the array
安全保障基于软件全生命周期-Istio的认证机制
Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
JWT login authentication + token automatic renewal scheme, well written!
Humiliation, resistance, reversal, 30 years, China should win Microsoft once