This commit is contained in:
朱子楚\zhuzi 2024-03-09 19:46:02 +08:00
parent a43637f75e
commit eff9e385f5
2 changed files with 28 additions and 25 deletions

View File

@ -18,6 +18,12 @@ else()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/release) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/release)
endif() endif()
if(APPLE)
set(APPLICATION_DIR_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}.app/Contents/MacOS)
else()
set(APPLICATION_DIR_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
endif()
#Qt #Qt
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Quick Svg Network) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Quick Svg Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Quick Svg Network) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Quick Svg Network)
@ -33,20 +39,20 @@ endif()
# #
find_program(QT_LUPDATE NAMES lupdate) find_program(QT_LUPDATE NAMES lupdate)
find_program(QT_LRELEASE NAMES lrelease) find_program(QT_LRELEASE NAMES lrelease)
if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/example_en.qm) if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}_en.qm)
execute_process(COMMAND ${QT_LUPDATE} ${CMAKE_CURRENT_LIST_DIR} -ts example_en.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}) execute_process(COMMAND ${QT_LUPDATE} ${CMAKE_CURRENT_LIST_DIR} -ts ${PROJECT_NAME}_en.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
execute_process(COMMAND ${QT_LRELEASE} example_en.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}) execute_process(COMMAND ${QT_LRELEASE} ${PROJECT_NAME}_en.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
endif () endif ()
if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/example_zh.qm) if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}_zh.qm)
execute_process(COMMAND ${QT_LUPDATE} ${CMAKE_CURRENT_LIST_DIR} -ts example_zh.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}) execute_process(COMMAND ${QT_LUPDATE} ${CMAKE_CURRENT_LIST_DIR} -ts ${PROJECT_NAME}_zh.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
execute_process(COMMAND ${QT_LRELEASE} example_zh.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}) execute_process(COMMAND ${QT_LRELEASE} ${PROJECT_NAME}_zh.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
endif () endif ()
file(GLOB QM_FILE_PATHS ${CMAKE_CURRENT_LIST_DIR}/ *.qm) file(GLOB QM_FILE_PATHS ${CMAKE_CURRENT_LIST_DIR}/ *.qm)
file(COPY ${QM_FILE_PATHS} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/i18n) file(COPY ${QM_FILE_PATHS} DESTINATION ${APPLICATION_DIR_PATH}/i18n)
## ##
set(HEADER_FILE_VERSION_PATH ${CMAKE_SOURCE_DIR}/example/Version.h) set(HEADER_FILE_VERSION_PATH ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/Version.h)
configure_file( configure_file(
${CMAKE_SOURCE_DIR}/.cmake/Version.h.in ${CMAKE_SOURCE_DIR}/.cmake/Version.h.in
${HEADER_FILE_VERSION_PATH} ${HEADER_FILE_VERSION_PATH}
@ -103,12 +109,12 @@ endif()
# #
if (WIN32) if (WIN32)
add_executable(example add_executable(${PROJECT_NAME}
${sources_files} ${sources_files}
${EXAMPLE_VERSION_RC_PATH} ${EXAMPLE_VERSION_RC_PATH}
) )
else () else ()
add_executable(example add_executable(${PROJECT_NAME}
${sources_files} ${sources_files}
) )
endif () endif ()
@ -132,12 +138,12 @@ if(WIN32)
endif() endif()
file(MAKE_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/source/) file(MAKE_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/source/)
file(COPY ${CMAKE_SOURCE_DIR}/src/ DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/source/) file(COPY ${CMAKE_SOURCE_DIR}/src/ DESTINATION ${APPLICATION_DIR_PATH}/source/)
if(QT_VERSION VERSION_GREATER_EQUAL "6.2") if(QT_VERSION VERSION_GREATER_EQUAL "6.2")
#Qt6.2使qt_add_qml_module #Qt6.2使qt_add_qml_module
qt_add_qml_module(example qt_add_qml_module(${PROJECT_NAME}
URI "example" URI ${PROJECT_NAME}
VERSION 1.0 VERSION 1.0
QML_FILES ${qml_files} QML_FILES ${qml_files}
RESOURCES ${resource_files} RESOURCES ${resource_files}
@ -145,27 +151,27 @@ if(QT_VERSION VERSION_GREATER_EQUAL "6.2")
) )
else() else()
#Qt6.2使qrc #Qt6.2使qrc
target_include_directories(example PRIVATE target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
) )
target_sources(example PRIVATE example.qrc) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}.qrc)
endif() endif()
#component,QML_NAMED_ELEMENTc++ #component,QML_NAMED_ELEMENTc++
target_include_directories(example PRIVATE target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/component ${CMAKE_CURRENT_SOURCE_DIR}/src/component
) )
#FluentUI.h #FluentUI.h
if(FLUENTUI_BUILD_STATIC_LIB) if(FLUENTUI_BUILD_STATIC_LIB)
target_include_directories(example PRIVATE target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/src
) )
endif() endif()
# #
set_target_properties(example PROPERTIES set_target_properties(${PROJECT_NAME} PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_GUI_IDENTIFIER my.${PROJECT_NAME}.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE MACOSX_BUNDLE TRUE
@ -173,13 +179,13 @@ set_target_properties(example PROPERTIES
) )
#Release #Release
target_compile_definitions(example target_compile_definitions(${PROJECT_NAME}
PRIVATE PRIVATE
QT_MESSAGELOGCONTEXT QT_MESSAGELOGCONTEXT
) )
# #
target_link_libraries(example PRIVATE target_link_libraries(${PROJECT_NAME} PRIVATE
Qt${QT_VERSION_MAJOR}::Quick Qt${QT_VERSION_MAJOR}::Quick
Qt${QT_VERSION_MAJOR}::Svg Qt${QT_VERSION_MAJOR}::Svg
Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::Network
@ -187,7 +193,7 @@ target_link_libraries(example PRIVATE
) )
# #
install(TARGETS example install(TARGETS ${PROJECT_NAME}
BUNDLE DESTINATION . BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}

View File

@ -19,9 +19,6 @@ void TranslateHelper::init(QQmlEngine* engine){
_translator = new QTranslator(this); _translator = new QTranslator(this);
qApp->installTranslator(_translator); qApp->installTranslator(_translator);
QString translatorPath = QGuiApplication::applicationDirPath()+"/i18n"; QString translatorPath = QGuiApplication::applicationDirPath()+"/i18n";
#ifdef Q_OS_MACX
translatorPath.append("/../Resources/");
#endif
_translator->load(QString::fromStdString("%1/example_%2.qm").arg(translatorPath,_current)); _translator->load(QString::fromStdString("%1/example_%2.qm").arg(translatorPath,_current));
_engine->retranslate(); _engine->retranslate();
} }