使用CMake构建

This commit is contained in:
2023-09-19 20:27:34 +08:00
parent b29bd9e878
commit 635b515a70
7 changed files with 96 additions and 70 deletions
+76
View File
@@ -0,0 +1,76 @@
cmake_minimum_required(VERSION 3.5)
project(SerialAsst 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)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS SerialPort)
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp mainwindow.h
mainwidget.cpp mainwidget.h
serialcontroller.cpp serialcontroller.h
)
set(app_icon_resource_windows "${CMAKE_CURRENT_SOURCE_DIR}/serialasst.rc")
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(SerialAsst
MANUAL_FINALIZATION
${PROJECT_SOURCES}
${app_icon_resource_windows}
resource.qrc
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET qtCmakeTemplate 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(SerialAsst 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(SerialAsst
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(SerialAsst PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(SerialAsst PRIVATE Qt6::SerialPort)
# 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.SerialAsst)
endif()
set_target_properties(SerialAsst 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 SerialAsst
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(SerialAsst)
endif()
-44
View File
@@ -1,44 +0,0 @@
#-------------------------------------------------
#
# Project created by QtCreator 2018-01-16T00:25:36
#
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = SerialAsst
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
serialcontroller.cpp \
mainwidget.cpp \
mainwindow.cpp
HEADERS += \
serialcontroller.h \
mainwidget.h \
mainwindow.h
RESOURCES += \
resource.qrc
DISTFILES += \
serialasst.rc
RC_FILE += \
serialasst.rc
+3 -3
View File
@@ -1,11 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setAttribute(Qt::AA_UseHighDpiPixmaps);
a.setWindowIcon(QIcon(QString(":/pic/icon/icon.png")));
a.setStyle("fusion");
MainWindow w;
w.show();
+4 -5
View File
@@ -109,7 +109,7 @@ MainWidget::MainWidget(QWidget *parent)
leftLlayout->addRow(COMLabel, COMBox);
leftLlayout->addRow(OpenButton);
leftLlayout->setAlignment(OpenButton, Qt::AlignVCenter);
leftLlayout->setMargin(30);
leftLlayout->setContentsMargins(30, 30, 30, 30);
leftLlayout->setHorizontalSpacing(20);
leftLlayout->setVerticalSpacing(60);
@@ -190,14 +190,13 @@ MainWidget::MainWidget(QWidget *parent)
void MainWidget::CheckSerials()
{
//不断检查可用串口列表,并与当前列表进行比较,若发生变化则重新生成列表
emit sendDateTime(QDateTime::currentDateTime().toString());//更新状态栏时间
emit sendDateTime(QDateTime::currentDateTime().toString(Qt::ISODate)); //更新状态栏时间
QList<QSerialPortInfo> SerialList = QSerialPortInfo::availablePorts();
if (!SerialList.isEmpty())
{
QStringList TmpComList, TmpPortNameList, TmpDesList;
for (QSerialPortInfo serial : SerialList)
{
for (QSerialPortInfo &serial : SerialList) {
TmpComList << serial.portName() + " " + serial.description();
TmpPortNameList << serial.portName();
TmpDesList << serial.description();
@@ -226,7 +225,7 @@ void MainWidget::CheckSerials()
COMBox->addItem(tr("(空)"));
COMBox->setDisabled(true);
OpenButton->setDisabled(true);
emit CloseSerial();
CloseSerial();
isOpened = false;
}
}
-4
View File
@@ -1,10 +1,6 @@
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#include <QWidget>
#include <QComboBox>
#include <QGridLayout>
+10 -7
View File
@@ -47,13 +47,16 @@ void MainWindow::showAbout()
QMessageBox *aboutWindow = new QMessageBox(this);
aboutWindow->setStandardButtons(QMessageBox::Ok);
aboutWindow->setText(tr("<h1>SerialAsst</h1>"
"<p>Based on Qt 5.10.1 (") +
getCompilerVersion() +
tr(")</p>"
"Source Code: <a href=\"https://github.com/c-my/SerialAsst\">https://github.com/c-my/SerialAsst</a><br/>"
"Email: "
"<address><a href=\"mailto:cmy1113@outlook.com?subject=SerialAsst Feedback\">cmy1113@outlook.com</a>"
"</address>"));
"<p>Based on Qt ")
+ QT_VERSION_STR + " (" + getCompilerVersion()
+ tr(")</p>"
"Source Code: <a "
"href=\"https://github.com/c-my/SerialAsst\">https://github.com/c-my/"
"SerialAsst</a><br/>"
"Email: "
"<address><a href=\"mailto:cmy1113@outlook.com?subject=SerialAsst "
"Feedback\">cai.my@outlook.com</a>"
"</address>"));
aboutWindow->show();
}
+3 -7
View File
@@ -1,16 +1,12 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#include <QKeyEvent>
#include <QMainWindow>
#include <QMenuBar>
#include <QStatusBar>
#include <QKeyEvent>
#include <QMessageBox>
#include <mainwidget.h>
#include <QStatusBar>
#include "mainwidget.h"
class MainWindow : public QMainWindow
{