CMake|VS2017+CMake3.8搭建Qt项目

试了半天才运行成功,和网络上好多都不太一样,持续学习CMAKE搭建工程中,欢迎交流。
部分代码参考网络。

第一步 选择CMake Project

在这里插入图片描述
点击OK后生成项目可直接运行
在这里插入图片描述

添加目录及文件

右键添加一个.h及.cpp文件
在这里插入图片描述
在这里插入图片描述
添加完后修改名称,添加include和src文件夹,修改目录结构,但要保证CMakeLists.txt和main函数在一个目录下

在这里插入图片描述
在这里插入图片描述

修改所有文件内容

修改CMakeLists.txt内容

在这里插入图片描述

# CMakeList.txt : CMake project for MVSImitateCMake, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)

# 设置C++标准 C++17
set(CMAKE_CXX_STANDARD 17)

# 自动把ui转化为C++代码
# uic qtcmake.ui > ui_qtcmake.h
set(CMAKE_AUTOUIC ON)

# 自动生成元对象的C++代码
set(CMAKE_AUTOMOC ON)

# 自动生成资源文件
set(CMAKE_AUTORCC ON)

# --- 执行文件输出路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

set(INCLUDE_DIR ./include)
set(SRC_DIR ./src)

include_directories(${INCLUDE_DIR})
file(GLOB_RECURSE HEADER "${INCLUDE_DIR}/*.h")
file(GLOB_RECURSE SOURCES "${SRC_DIR}/*.cpp")

# Add source to this project's executable.
add_executable (CMakeProject1 CMakeProject1.cpp ${HEADER} ${SOURCES})

# TODO: Add tests and install targets if needed.
# find_package 查找内部库
# 导入qt的库
# cmake通过qt5提供的查找方案,去查找对应的库
# 这里以查找 Widgets库 为例
find_package(Qt5 COMPONENTS Widgets REQUIRED)

# 根据自己电脑的环境,写死的指定Qt5_DIR这个变量
# 目的是寻找 Qt5Config.cmake 这个文件
set(Qt5_DIR C:/Qt/Qt5.14.2/5.14.2/msvc2017_64/lib/cmake/Qt5)

# 指定qt依赖的动态库
# Qt5 自带连接头文件
target_link_libraries(${PROJECT_NAME}
    Qt5::Widgets
)

修改主函数内容

在这里插入图片描述

// CMakeProject1.cpp : Defines the entry point for the application.
//

#include "CMakeProject1.h"

using namespace std;

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	MainWindow main_window;
	main_window.show();
	return a.exec();
	// 	cout << "Hello CMake." << endl;
	// 	system("pause");
	// 	return 0;
}

修改头文件

CMakeProject1.h

// CMakeProject1.h : Include file for standard system include files,
// or project specific include files.

#pragma once

#include <iostream>
#include <QtWidgets>
#include <QApplication>

#include "MainWindow.h"

// TODO: Reference additional headers your program requires here.

MainWindow.cpp

#pragma once
#include <qmainwindow.h>

class MainWindow :
	public QMainWindow
{
	Q_OBJECT
public:
	MainWindow();
	~MainWindow();

private:
	void Init();
};

修改类cpp文件

#include "MainWindow.h"
#include<QMenuBar>
#include<QToolBar>
#include <QLabel>
#include <QStatusBar>
#include <QHBoxLayout>

MainWindow::MainWindow()
{
	Init();
}


MainWindow::~MainWindow()
{
}

void MainWindow::Init()
{
	resize(1700, 950);
	QWidget* main_widget = new QWidget(this);
	this->setCentralWidget(main_widget);

	//包含菜单栏,只能有一个
	QMenuBar * bar = menuBar();
	//将菜单栏放入到窗口中
	this->setMenuBar(bar);

	//创建文件菜单
	QMenu * dockWidgetMenu = bar->addMenu(QStringLiteral("窗口"));
	//QMenu * editMenu = bar->addMenu(QStringLiteral("编辑"));

	//添加菜单项
	QAction * deviceAction = dockWidgetMenu->addAction(QStringLiteral("设备列表"));
	deviceAction->setCheckable(true);

	//添加分割线
	//dockWidgetMenu->addSeparator();
	QAction * ImageAction = dockWidgetMenu->addAction(QStringLiteral("图像显示"));
	ImageAction->setCheckable(true);

	//添加分割线
	//dockWidgetMenu->addSeparator();
	QAction * AttributeAction = dockWidgetMenu->addAction(QStringLiteral("属性信息"));
	AttributeAction->setCheckable(true);

	//工具栏,可以有多个
	QToolBar * toolBar = new QToolBar(this);
	addToolBar(Qt::TopToolBarArea, toolBar);//默认停靠范围

	//只允许左右侧停靠
	//toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);

	//设置浮动
	toolBar->setFloatable(false);

	//设置移动(总开关)
	toolBar->setMovable(false);

	//工具栏添加菜单项
	toolBar->addAction(deviceAction);
	//添加分割线
	toolBar->addSeparator();
	toolBar->addAction(ImageAction);
	toolBar->addSeparator();
	toolBar->addAction(AttributeAction);

	//状态栏,只能有一个
	QStatusBar * stBar = statusBar();
	setStatusBar(stBar);
	QLabel * label = new QLabel(QStringLiteral("提示信息"), this);
	stBar->addWidget(label);//添加提示信息到左侧
	QLabel * label2 = new QLabel(QStringLiteral("右侧提示信息"), this);
	stBar->addPermanentWidget(label2);

}

运行工程

第一次运行,选择CMakeLists.txt打开文件修改启动项
在这里插入图片描述

可直接运行起来
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奇树谦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值