我试图构建简单的Qt Quick项目,并希望在Qt Creator
之外使用它。这意味着仅使用Vim
或CMake
等工具编码,构建和运行程序。
我有两个问题:
- 我不明白为什么CMake不能自己找到所有需要的库并构建项目。
- 我不明白为什么在Qt Creator中单击
▶
按钮可以成功构建默认项目,但是自己运行CMake会导致c ++错误。
首先,我制作了CMakeLists.txt在github上找到的Qt5 Quick项目的图像中。然后我尝试通过 Creator 制作项目,然后检查它如何构建CMakeLists.txt
并自己编写一个。
我的第一次尝试:
cmake_minimum_required(VERSION 3.11..3.15)
set(PROJECT_NAME "uint32_sort_gui")
project(${PROJECT_NAME} LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5 COMPONENTS Core Gui Qml Quick QuickControls2 REQUIRED)
add_subdirectory(${PROJECT_SOURCE_DIR}/sort_lib/)
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::Core
Qt5::Gui
Qt5::Qml
Qt5::Quick
Qt5::QuickControls2 #(*)
SortCore
)
set_target_properties(${PROJECT_NAME}
PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED ON)
第二个(由Qt Creator自动生成):
cmake_minimum_required(VERSION 3.1)
project(Deleteme2 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc")
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick)
首次上市失败(*)
行,说
Could not find a package configuration file provided by "Qt5QuickControls2"
with any of the following names:
Qt5QuickControls2Config.cmake
qt5quickcontrols2-config.cmake
Add the installation prefix of "Qt5QuickControls2" to CMAKE_PREFIX_PATH or
set "Qt5QuickControls2_DIR" to a directory containing one of the above
files. If "Qt5QuickControls2" provides a separate development package or
SDK, be sure it has been installed.
(但我已经安装了所有内容)
第二个在编译过程中失败并出错:
make
[ 16%] Automatic MOC for target Deleteme2
[ 16%] Built target Deleteme2_autogen
[ 33%] Building CXX object CMakeFiles/Deleteme2.dir/main.cpp.o
Deleteme2/main.cpp: In function ‘int main(int, char**)’:
Deleteme2/main.cpp:6:36: error: ‘AA_EnableHighDpiScaling’ is not a member of ‘Qt’
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
我做错了什么?为什么Qt Creator可以毫无问题地构建Deleteme2
?
编辑: 这是示例代码,由Qt Creator生成,用于第二次尝试
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
主要问题,为什么"第一次尝试"失败是因为Qt5将自己安装在~/Qt
和CMake在/usr/lib/x86_64-linux-gnu/cmake/
中搜索了*.cmake
配置文件(我没有意识到我使用预装了cmake 3.5.1的Ubuntu 16 LTS
)。
所以解决这个问题的方法是复制所有内容
~/Qt/*last cmake version*/gcc_64
(就我而言~/Qt/5.13.0/gcc_64
)
通过sudo cp -a ~/Qt/5.13.0/gcc_64/* /usr/lib/
进入/usr/lib/
第二个答案很短:
发生这种情况是因为默认情况下Qt Creator将CMake输出设置为Ninja makefiles。
转到Tools > Options > Kits
并添加所需的配置或编辑现有配置。