本文主要是介绍qt-C++笔记之使用Cmake来组织和构建QWidget工程项目,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
qt-C++笔记之使用Cmake来组织和构建QWidget工程项目
—— 杭州 2024-03-10
code review!
文章目录
- qt-C++笔记之使用Cmake来组织和构建QWidget工程项目
- 1.运行
- 2.文件结构
- 3.CMakeLists.txt
- 4.main.cpp
- 5.widget.h
- 6.widget.cpp
- 7.widget.ui
1.运行
2.文件结构
3.CMakeLists.txt
代码
cmake_minimum_required(VERSION 3.5)project(qt_cmake_widget LANGUAGES CXX)set(CMAKE_INCLUDE_CURRENT_DIR ON)set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()find_package(Qt5 COMPONENTS Widgets REQUIRED)if(ANDROID)add_library(qt_cmake_widget SHAREDmain.cppwidget.cppwidget.hwidget.ui)
else()add_executable(qt_cmake_widgetmain.cppwidget.cppwidget.hwidget.ui)
endif()target_link_libraries(qt_cmake_widget PRIVATE Qt5::Widgets)
4.main.cpp
#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
5.widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;
};
#endif // WIDGET_H
6.widget.cpp
#include "widget.h"
#include "./ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}
7.widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>Widget</class><widget class="QWidget" name="Widget"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="windowTitle"><string>Widget</string></property></widget><resources/><connections/>
</ui>
这篇关于qt-C++笔记之使用Cmake来组织和构建QWidget工程项目的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!