windows7 qt5 make vs 配合使用
项目目录
C:\USERS\ADMINISTRATOR.USER-20190802BR\DESKTOP\ONLINECHAT
│ CMakeLists.txt
│ main.cpp
│ main.qml
│ qml.qrc
│
└─build
主要文件
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(01_qml LANGUAGES CXX)
set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.13.1/5.13.1/msvc2017_64/lib/cmake/Qt5")
set(Qt5 "C:/Qt/Qt5.13.1/5.13.1/msvc2017_64/lib/cmake/Qt5")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(HEADERS
#appglobal.h
)
set(SOURCES
main.cpp
#appglobal.cpp
)
set(RESOURCES
qml.qrc
)
set(QMLS
main.qml
)
find_package(Qt5 COMPONENTS Core Gui Qml Quick REQUIRED)
get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
# [自动部署原文](https://zhuanlan.zhihu.com/p/73469206)
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
find_program(DEPLOYQT_EXECUTABLE NAMES windeployqt macdeployqt HINTS "${_qt_bin_dir}")
if(APPLE)
add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${SOURCES} ${RESOURCES} ${QMLS})
configure_file(${CMAKE_SOURCE_DIR}/Info.plist.in ${CMAKE_BINARY_DIR}/Info.plist)
set_target_properties(${PROJECT_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_BINARY_DIR}/Info.plist)
add_custom_command(TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${DEPLOYQT_EXECUTABLE} "$<TARGET_BUNDLE_DIR:${PROJECT_NAME}>" -qmldir=${CMAKE_SOURCE_DIR}
)
elseif(WIN32)
add_executable(${PROJECT_NAME} WIN32 ${SOURCES} ${RESOURCES} ${QMLS})
add_custom_command(TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${DEPLOYQT_EXECUTABLE} "$<TARGET_FILE:${PROJECT_NAME}>"
--$<LOWER_CASE:$<CONFIG>> --qmldir ${CMAKE_SOURCE_DIR}
)
endif()
target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Gui Qt5::Qml Qt5::Quick)
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
}
qml.qrc
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::addLibraryPath("./");
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
//getchar();
//if (engine.rootObjects().isEmpty())
// return -1;
return app.exec();
}
编译
进入build目录执行以下命令
cmake .. -G "Visual Studio 16 2019"
cmake --build . -- /p:Configuration=Release