1

Im trying to build simple Qt Quick project and want to work with it outside Qt Creator. It means coding, building and running program only with tools like Vim or CMake.

I have two problems:

  1. I don't understand why CMake cannot find all needed libraries by itself and build project.
  2. I don't understand why clicking on button in Qt Creator builds default project succesfully, but running CMake by myself results in c++ error.

Firstly i made CMakeLists.txt in the image of Qt5 Quick project i found on github. Then I tried to make project via Creator, then inspect how it builds CMakeLists.txt and compose one by myself.

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):

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?

Edited: Here is the sample code, generated by Qt Creator for second attempt

#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();
}

1 Answers1

0

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).

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)

into /usr/lib/ via 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.

Go to Tools > Options > Kits and add desired configuration or edit existing.