Mix|使用VS2017CMake构建Qt工程 仿照MVS(仅用于学习)

MVS下载链接:https://www.hikrobotics.com/cn/machinevision/service/download/?module=0
CMake工程构建参考:CMake|VS2017+CMake3.8搭建Qt项目

效果图

只用Qt实现了显示效果,并没有实现具体功能,仅供学习使用,如有侵权请私信我删除。
在这里插入图片描述

整体结构

在这里插入图片描述

实现代码

最外层CMakeLists.txt

# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)

project ("MVSImitateCMake")

# Include sub-projects.
add_subdirectory ("MVSImitateCMake")

代码实现及CMakeLists.txt搭建

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 (MVSImitateCMake MVSImitateCMake.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
)

主函数

头文件

// MVSImitateCMake.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.

源文件

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

#include "MVSImitateCMake.h"

using namespace std;

int main(int argc, char *argv[])
{
   
   
	QApplication a(argc, argv);
	MainWindow main_window;
	main_window.show();
	return a.exec();
}

从主函数引用顺序来展示各个类的代码

MainWindow

搭建整体UI架构,创建DeviceListDockWidget类、ImageShowDockWidget类、AttributeListDockWidget类,分别为设备列表、图像显示区域、属性窗口区域。
头文件

#pragma once
#include <qmainwindow.h>
#include <QSplitter>
#include "DeviceList.h"
#include "ImageShow.h"
#include "AttributeList.h"

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

private:
	void Init();

	DeviceListDockWidget* device_list_;
	ImageShowDockWidget* image_show_;
	AttributeListDockWidget* attribute_list_;
};

源文件

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

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


MainWindow::~MainWindow()
{
   
   
}

void MainWindow::Init()
{
   
   
	resize(1700, 950);
	QSplitter* main_widget = new QSplitter(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);

	device_list_ = new DeviceListDockWidget;
	QHBoxLayout* main_layout = new QHBoxLayout;
	main_layout->addWidget(device_list_,1);

	image_show_ = new ImageShowDockWidget;
	main_layout->addWidget(image_show_,5);

	attribute_list_ = new AttributeListDockWidget;
	main_layout->addWidget(attribute_list_, 2);

	main_widget->setLayout(main_layout);


	// 设备列表显隐
	connect(deviceAction, &QAction::toggled, device_list_, [=](bool state) {
   
   
		device_list_->setHidden(!state);
	});
	connect(device_list_, &QDockWidget::visibilityChanged, deviceAction, &QAction::setChecked);
	// 图像显示区域显隐
	connect(ImageAction, &QAction::toggled, image_show_, [=](bool state) {
   
   
		image_show_->setHidden(!state);
	});
	connect(image_show_, &QDockWidget::visibilityChanged, ImageAction, &QAction::setChecked);;
	// 属性列表显隐
	connect(AttributeAction, &QAction::toggled, attribute_list_, [=](bool state) {
   
   
		attribute_list_->setHidden(!state);
	});
	connect(attribute_list_, &QDockWidget::visibilityChanged, AttributeAction, &QAction::setChecked);
}

DeviceListDockWidget

设备列表类继承QDockWidget类
头文件

#pragma once
#include <QApplication>
#include <QClipboard>
#include <QDockWidget>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QVBoxLayout>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QToolBar>
#include <QLabel>
#include <QStatusBar>
#include <QLabel>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QSplitter>
#include <QHeaderView>
#include <Struct.h>

class DeviceListDockWidget :
	public QDockWidget
{
   
   
	Q_OBJECT
public:
	DeviceListDockWidget();
	~DeviceListDockWidget();

private:
	void Init();
	void InitSlot();
	void InitDeviceInfoTable();

	void AddGigECameraTreeItem(QString _camera_name);
	void AddUSBCameraTreeItem(QString _camera_name);
	
	void RefreshDeviceInfoTable(QString _camera_name,DeviceInfo _device_info);
	void RefreshGigEInfo(GigEDeviceInfo _gige_info);
	void RefreshUSBInfo(USBDeviceInfo _usb_info);

	DeviceInfo GetDeviceInfo(QString _camera_name);

private slots:
	void SlotClickedDeviceList(QTreeWidgetItem* _item);
	void SlotShowMenuPos(const QPoint &pos);
	void SlotMenu(QAction* _action);

private:
	//QMap<QString, DeviceInfo> all_device;
	QVBoxLayout* main_layout;
	QTreeWidget *device_list;
	QTreeWidgetItem* GigE_root;
	QTreeWidgetItem* USB_root;
	QTableWidget* device_info_table;
	QMenu* copy_menu;
	QAction* copy_select;
	QAction* copy_all;
};

源文件

#include "DeviceList.h"

DeviceListDockWidget::DeviceListDockWidget()
{
   
   
	Init()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

奇树谦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值