Build Qt project using CMake

Build Qt project using CMake

  • To add more functions to the new GUI design, QtCreator has been installed on Windows, and I don’t want to install another one under Ubuntu, so I developed it under Windows and compiled and run it under Linux, so I used CMake to build the Qt project.

1. qmake in Qt

  • qmake can use source files, header files, qt ui files, etc. to generate various types of projects and Makefiles required by projects.
  • pro file, add various source files, you can also set different source files related to the platform, set various rules, and use the qmake command to generate the project.

Compile in QT in three steps:

  • qmake -project (used to create .pro files and compile all files into a platform-independent project file)
  • qmake (read its own Qt settings, and generate the corresponding Makefile consistent with the library)
  • make (according to the generated Makefile, compile the file into a binary executable program)

2. Build in CMake

  • The parameter used in CMake refers to the directory where the CMakeLists.txt file is located. This CMakeLists.txt file controls the entire compilation process. In order to understand it more thoroughly, we use the following diagram to look at the entire compilation process. The following picture shows how the files written by users (source code, header files, .ui files, .qrc files) are processed by Qt tools during the compilation process and integrated into the entire compilation process. Because qmake is used to handle this process, it hides many details in this process.

    img

  • Qt does not use the "standard" C++ language, but "extends" it to a certain extent. Here we can see from the newly added keywords of Qt: signals, slots or emit. So some people think that Qt's program compilation speed is slow. This is mainly because these extended syntax needs to be removed before Qt delivers the source code to a standard C++ compiler, such as gcc. What completes this operation is the moc.

  • In Qt, if a class wants to use the signal/slot function, it must declare Q_OBJECT in it. Only by adding Q_OBJECT, you can use the signal and slot mechanism in QT.

    At this time, when the header file derives the class, first introduce the Q_OBJECT macro as follows:
    class MyMainWindow : public QWidget
    { Q_OBJECT }


  • moc is a precompiler for QT, used to process slot, signal, emit, Q_OBJECT, etc. in the code. The moc file is the corresponding processing code, which is the implementation part of the Q_OBJECT macro

  • If the Q_OBJECT macro is used in the header file, the file needs to be processed by moc, the .ui file must also be processed by uic, and the .qrc file needs to be processed by the rcc program.

3. Specific construction process

  • The following is the minesweeper program file I wrote under windows (including .ui.qrc)insert image description here

  • Below is the content of CMakeList.txt.

#cmake版本
cmake_minimum_required(VERSION 3.5.1)

#项目名称
project(Minesweeper)

#让cmake自动去寻找Qt5
FIND_PACKAGE(Qt5 COMPONENTS Widgets REQUIRED)

#采用C++11标准
set(CMAKE_CXX_STANDARD 11)

#添加所有的源文件
SET(Minesweeper_SOURCES 
	dialog.cpp	
	main.cpp 
	mainwindow.cpp 
	minemap.cpp
	myitem.cpp
	myscene.cpp
	setpro.cpp
)

#添加所有的头文件
SET(Minesweeper_HEADERS 
	dialog.h
	mainwindow.h
	minemap.h
	myitem.h
	myscene.h
	setpro.h
)

#添加所有的.ui文件
SET(Minesweeper_FORMS 
	dialog.ui
	mainwindow.ui
	setpro.ui
)

#添加资源文件
SET(Minesweeper_RESOURCES 
	img.qrc
)
#调用预编译器moc,需要使用 QT5_WRAP_CPP宏
QT5_WRAP_CPP(Minesweeper_HEADERS_MOC ${
    
    Minesweeper_HEADERS})
#使用uic处理.ui文件
QT5_WRAP_UI(Minesweeper_FORMS_HEADERS ${
    
    Minesweeper_FORMS})
#使用rcc处理.qrc文件
QT5_ADD_RESOURCES(Minesweeper_RESOURCES_RCC ${
    
    Minesweeper_RESOURCES})

#这些生成的中间文件都会在build目录下,这样的话,编译器则不能定位由uic程序产生的诸如_ui_mainwindow.h等文件。所以,我们需要把build目录添加到包含目录中
INCLUDE_DIRECTORIES(${
    
    CMAKE_CURRENT_BINARY_DIR})

#生成可执行文件
ADD_EXECUTABLE(Minesweeper 
	${
    
    Minesweeper_SOURCES} 
	${
    
    Minesweeper_HEADERS_MOC} 
    ${
    
    Minesweeper_FORMS_HEADERS}
	${
    
    Minesweeper_RESOURCES_RCC} 
)

#为target添加需要链接的共享库
TARGET_LINK_LIBRARIES(Minesweeper ${
    
    Qt5Widgets_LIBRARIES})
  • The following is the result of the build:

insert image description here

  • Here is the result after running:

insert image description here

4. Ending

  • With the above construction, the addition of other resources can also be completed, and a good interface and function can be developed in combination with the new bottom layer.

Guess you like

Origin blog.csdn.net/qq_40181592/article/details/101623231