Qt开发,使用Qt for Python还是Qt C++?

发布时间:
2023-08-24 12:32
阅读量:
14

背景

最近公司需要用海康摄像头搞事情,之前用WPF、VS2022/c++做过一版,现在想要QT来折腾一下。

环境:QT6.5.2LTS+CMAKE+WIN10+MSVC/MINGW

参阅了一堆资料后发现,用QT+CMake的资料很少,零星有几个也是藏着捂着的,自己琢磨了一天给搞出来了,遂向大家汇报一下。

先说明一下,本人QT和CMake都很菜,欢迎大佬莅临指正打脸!

文件结构

整体

Lib文件夹

Include文件夹

一堆动态库放在编译文件夹

代码

CMAKE

cmake_minimum_required(VERSION 3.5) project(HivisionControl VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) set(PROJECT_SOURCES main.cpp mainwindow.cpp mainwindow.h mainwindow.ui ) set(SDK_DIR "${PROJECT_SOURCE_DIR}/Include") set(LIB_DIR "C:\\QTProjects\\HivisionControl\\Lib") include_directories(${SDK_DIR}) link_directories(${LIB_DIR}) add_library(HCNetSDKLib STATIC IMPORTED) set_target_properties(HCNetSDKLib PROPERTIES IMPORTED_LOCATION "${LIB_DIR}/HCNetSDK.lib") add_library(HCCoreLib STATIC IMPORTED) set_target_properties(HCCoreLib PROPERTIES IMPORTED_LOCATION "${LIB_DIR}/HCCore.lib") add_library(HCPreviewLib STATIC IMPORTED) set_target_properties(HCPreviewLib PROPERTIES IMPORTED_LOCATION "${LIB_DIR}/HCPreview.lib") add_library(PlayCtrlLib STATIC IMPORTED) set_target_properties(PlayCtrlLib PROPERTIES IMPORTED_LOCATION "${LIB_DIR}/PlayCtrl.lib") add_library(HCGeneralCfgMgrLib STATIC IMPORTED) set_target_properties(HCGeneralCfgMgrLib PROPERTIES IMPORTED_LOCATION "${LIB_DIR}/HCGeneralCfgMgr.lib") add_library(HCAlarmLib STATIC IMPORTED) set_target_properties(HCAlarmLib PROPERTIES IMPORTED_LOCATION "${LIB_DIR}/HCAlarm.lib") if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(HivisionControl WIN32 MANUAL_FINALIZATION ${PROJECT_SOURCES} ) #add_executable(HivisionControl WIN32 ${PROJECT_SOURCES}) # Define target properties for Android with Qt 6 as: # set_property(TARGET HivisionControl APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR # ${CMAKE_CURRENT_SOURCE_DIR}/android) # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation else() if(ANDROID) add_library(HivisionControl SHARED ${PROJECT_SOURCES} ) # Define properties for Android with Qt 5 after find_package() calls as: # set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android") else() add_executable(HivisionControl ${PROJECT_SOURCES} ) endif() endif() target_link_libraries(HivisionControl PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) target_include_directories(HivisionControl PRIVATE ${SDK_DIR}) target_link_libraries(HivisionControl PRIVATE HCNetSDKLib HCCoreLib HCPreviewLib PlayCtrlLib HCGeneralCfgMgrLib HCAlarmLib) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. # If you are developing for iOS or macOS you should consider setting an # explicit, fixed bundle identifier manually though. if(${QT_VERSION} VERSION_LESS 6.1.0) set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.HivisionControl) endif() set_target_properties(HivisionControl PROPERTIES ${BUNDLE_ID_OPTION} MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) include(GNUInstallDirs) install(TARGETS HivisionControl BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) if(QT_VERSION_MAJOR EQUAL 6) qt_finalize_executable(HivisionControl) endif()

mainwindow.h

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; long m_lPlayHandle = 0; private slots: int LoginBtn_Cliked(); }; #endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h" #include "./ui_mainwindow.h" #include "HCNetSDK.h" #include <windows.h> #include <qdebug.h> #include <QGraphicsView> #include <QWidget> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->loginBtn, &QPushButton::clicked, this, &MainWindow::LoginBtn_Cliked); } MainWindow::~MainWindow() { delete ui; } int MainWindow::LoginBtn_Cliked() { qDebug() << "begin login"; bool isInit = NET_DVR_Init(); if (isInit) { qDebug() << "SDK Initialized!"; NET_DVR_SetConnectTime(2000, 3); NET_DVR_SetReconnect(10000, true); NET_DVR_USER_LOGIN_INFO struLoginInfo = {0}; strcpy_s(struLoginInfo.sDeviceAddress, "43"); strcpy_s(struLoginInfo.sUserName, "admin"); strcpy_s(struLoginInfo.sPassword, "43"); struLoginInfo.wPort = 8000; NET_DVR_DEVICEINFO_V40 struDeviceInfo = {0}; int lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfo); if (lUserID < 0) { qDebug() << "login error: " << NET_DVR_GetLastError(); NET_DVR_Cleanup(); return -1; } else { qDebug() << "login success"; HWND hWND = (HWND)ui->graphicsView->winId(); NET_DVR_PREVIEWINFO structPlayInfo; structPlayInfo.hPlayWnd = hWND; structPlayInfo.lChannel = 1; structPlayInfo.dwStreamType = 0; structPlayInfo.dwLinkMode = 0; structPlayInfo.bBlocked = 1; m_lPlayHandle = NET_DVR_RealPlay_V40(lUserID, &structPlayInfo, nullptr, nullptr); if (m_lPlayHandle < 0) { qDebug() << "preview error: " << NET_DVR_GetLastError(); NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return -1; } } } else { qDebug() << "SDK init failed: " << NET_DVR_GetLastError(); } ui->loginBtn->setText("Logout"); return 1; }

界面

最后

现在展示的是预览功能,其他还有很多功能,看官方文件就知道了,SDK在官网下载。

END