[英]Building Qt5 Quick project without Qt Creator via CMake
Im trying to build simple Qt Quick project and want to work with it outside Qt Creator
. 我试图建立一个简单的Qt Quick项目,并想在
Qt Creator
之外使用它。 It means coding, building and running program only with tools like Vim
or CMake
. 这意味着只能使用
Vim
或CMake
类的工具来编码,构建和运行程序。
I have two problems: 我有两个问题:
▶
button in Qt Creator builds default project succesfully, but running CMake by myself results in c++ error. ▶
按钮会成功构建默认项目,但是我自己运行CMake会导致c ++错误。 Firstly i made CMakeLists.txt in the image of Qt5 Quick project i found on github. 首先,我在github上找到的Qt5 Quick项目的图像中制作了CMakeLists.txt。 Then I tried to make project via Creator, then inspect how it builds
CMakeLists.txt
and compose one by myself. 然后,我尝试通过Creator创建项目,然后检查它如何构建
CMakeLists.txt
并由我自己撰写。
My first attempt: 我的第一次尝试:
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)
Second (autogenerated by Qt Creator): 第二(由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)
First listing fails on (*)
line, saying 第一次上市失败
(*)
行,说
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.
(But i've installed everything) (但我已经安装了一切)
And second fails with error during compilation: 第二个失败,并在编译期间出现错误:
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);
What am i doing wrong? 我究竟做错了什么? Why Qt Creator can build
Deleteme2
without a problem? 为什么Qt Creator可以毫无问题地构建
Deleteme2
?
Edited: Here is the sample code, generated by Qt Creator for second attempt 编辑:这是示例代码,由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();
}
The main problem, why "First attempt" failed is because Qt5 installed itself in ~/Qt
and CMake searched for *.cmake
configuration files in /usr/lib/x86_64-linux-gnu/cmake/
(I didn't metion that i use Ubuntu 16 LTS
, which have cmake 3.5.1 preinstalled). 主要问题为何“首次尝试”失败是因为Qt5将自身安装在
~/Qt
并且CMake在/usr/lib/x86_64-linux-gnu/cmake/
搜索*.cmake
配置文件(我没有提到我请使用预先安装了cmake 3.5.1的Ubuntu 16 LTS
)。
So the way to solve this problem was to copy everything that was in 因此,解决此问题的方法是复制其中的所有内容。
~/Qt/*last cmake version*/gcc_64
(in my case ~/Qt/5.13.0/gcc_64
) ~/Qt/*last cmake version*/gcc_64
(在我的情况下~/Qt/5.13.0/gcc_64
)
into /usr/lib/
via sudo cp -a ~/Qt/5.13.0/gcc_64/* /usr/lib/
入
/usr/lib/
通过sudo cp -a ~/Qt/5.13.0/gcc_64/* /usr/lib/
The answer to second is short: 第二个答案很短:
This happend because by default Qt Creator sets CMake output into Ninja makefiles. 发生这种情况是因为默认情况下,Qt Creator将CMake输出设置为Ninja makefile。
Go to Tools > Options > Kits
and add desired configuration or edit existing. 转到
Tools > Options > Kits
然后添加所需的配置或编辑现有配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.