修改已有工程

更新时间:
2024-01-09
下载文档

修改已有工程

为了适配 SylixOS,工程模版做了一些修改,因此如果用户是导入以前创建的工程,需要在工程文件中添加一些配置。

QMake

启用自动化部署

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
sylixos: target.path = /apps/$${TARGET}
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

此处用户可根据自己的需求,部署任意文件到 SylixOS 设备,例如:

deploy.files = /path/to/file        # 这里可以以空格列出多个文件或目录
deploy.path  = /path/to/deploy      # 这里是部署到设备的路径
INSTALLS += deploy                  # 加入到部署列表

启用 qwt

# To use Qwt in your project, enable the USE_QWT=1.
USE_QWT = 1
!isEmpty(USE_QWT) {
    LIBS += -L$$[QT_INSTALL_PREFIX]\lib -lqwt
    INCLUDEPATH += $$[QT_INSTALL_PREFIX]\include\Qwt
}

完整模板示例

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# To use Qwt in your project, enable the USE_QWT=1.
#USE_QWT = 1
!isEmpty(USE_QWT) {
    LIBS += -L$$[QT_INSTALL_PREFIX]\lib -lqwt
    INCLUDEPATH += $$[QT_INSTALL_PREFIX]\include\Qwt
}

# Default rules for deployment.
sylixos: target.path = /apps/$${TARGET}
else: qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

CMake

启用自动化部署

enable_auto_deploy(ON)
add_deployment_file(test "apps/${PROJECT_NAME}")

其中各个方法的使用方式如下。

方法名称描述参数
enable_auto_deploy开启或关闭自动化部署一个参数,可选ON(开启自动部署)或OFF(关闭自动部署)。
add_deployment_file
添加一个部署文件两个参数,参数1为待部署文件,需要填写以构建目录为参考的相对路径,参数2为部署目录,不需要写根目录的“\”。
add_deployment_directory添加一个部署目录两个参数,参数1为待部署目录,需要填写以构建目录为参考的相对路径,参数2为部署目录,不需要写根目录的“\”。

使用以上方法,可以添加任意文件或目录到部署列表。

启用 qwt

set(USE_QWT OFF)
if (USE_QWT)
    list(APPEND INCLUDE_PATHS ${CMAKE_PREFIX_PATH}/include/Qwt)
    list(APPEND LINK_PATHS ${CMAKE_PREFIX_PATH}/lib)
    set(QWT_LIB qwt)
endif()

完整模板示例

cmake_minimum_required(VERSION 3.5)

project(app VERSION 0.1 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)

set(INCLUDE_PATHS ${SYLIXOS_INCLUDE_PATHS})
set(LINK_PATHS ${SYLIXOS_LIB_PATHS})

set(USE_QWT OFF)
if (USE_QWT)
    list(APPEND INCLUDE_PATHS ${CMAKE_PREFIX_PATH}/include/Qwt)
    list(APPEND LINK_PATHS ${CMAKE_PREFIX_PATH}/lib)
    set(QWT_LIB qwt)
endif()

include_directories(${INCLUDE_PATHS})
link_directories(${LINK_PATHS})

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)

set(PROJECT_SOURCES
        main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(app 
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET testCMake 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(app 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(app
            ${PROJECT_SOURCES}
        )
    endif()
endif()

target_link_libraries(app PRIVATE ${SYLIXOS_LIBS} Qt${QT_VERSION_MAJOR}::Widgets ${QWT_LIB})

set_target_properties(app PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(app )
endif()

enable_auto_deploy(ON)
add_deployment_file(app "apps/${PROJECT_NAME}")

文档内容是否对您有所帮助?
有帮助
没帮助