9

I am willing to use styling within Qt Quick Controls 2 and together with a CMake project in C++. And I am having a hard time to get the colors right.

The C++, qml and styling code comes from the qt blog and is working fine so long as I use a .pro project file but when I turn to CMakeLists.txt I do not manage to get the colors right (I believe they are the default dark / purple colors rather than dark / orange).

In the .pro project my main.cpp looks like this:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickStyle>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    QQuickStyle::setStyle("Material");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));

    return app.exec();
}

My qml.qrc file lookes like this:

<!DOCTYPE RCC>
<RCC>
    <qresource prefix="/">
        <file>main.qml</file>
        <file>CustomLabel.qml</file>
        <file>PageBackground.qml</file>
        <file>SideBar.qml</file>
        <file>SideBarForm.ui.qml</file>
        <file>Light.qml</file>
        <file>LightForm.ui.qml</file>
        <file>Heating.qml</file>
        <file>HeatingForm.ui.qml</file>
        <file>Security.qml</file>
        <file>SecurityForm.ui.qml</file>
        <file>qtquickcontrols2.conf</file>
    </qresource>
</RCC>

And my qtquickcontrols2.conf file like this:

[Controls]
Style=Material

[Universal]
Theme=Dark
Accent=DeepOrange

[Material]
Theme=Dark
Accent=DeepOrange

Now my CMake project folder contains exactly the same files (it is actually a copy and paste of the .pro project folder) except that I have removed the .pri and .pro files and it now contains a CMakeLists.txt which looks like this:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.9)
cmake_policy(SET CMP0015 NEW)

# Projet. 
project(MyQtQuick)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# Find dependencies.
set(QT_DIR ../DevRoot/Dependencies/Qt/v5.7.0/5.7/gcc_64)

# Prepare project for Qt. 
set(CMAKE_INCLUDE_CURRENT_DIR ON) # Find includes in corresponding build directories.
set(CMAKE_AUTOMOC ON) # Instruct CMake to run moc automatically when needed.
set(CMAKE_PREFIX_PATH ${QT_DIR})

find_package(Qt5Widgets)
find_package(Qt5Core)
find_package(Qt5Gui)
find_package(Qt5OpenGL)
find_package(Qt5Quick REQUIRED)
find_package(Qt5Qml REQUIRED)
find_package(Qt5QuickControls2 REQUIRED)

# List sources.
set(${PROJECT_NAME}_sources
    main.cpp
)

qt5_add_resources(RCC_SOURCES qml.qrc)

# Headers.
include_directories(
    .
    )

# Output library.
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_sources})

# Linker.
qt5_use_modules(${PROJECT_NAME} Core Gui OpenGL QuickControls2)

Files main.cpp, qml.qrc and ****qtquickcontrols2.conf** remain the same (except for the path to main.qml in main.cpp which I had to change from "qrc:/main.qml" to "../main.qml" as **qrc: does not work with CMake - no idea why).

Now when I compile and execute the CMake project I get the application executed but the colors are bad, the application is rendered with the default dark/pink colors rather the dark/orange colors defined in qtquickcontrols2.conf. My .pro project compiles and runs smoothly though.

I have looked for hours over the internet and could not find any answer. I have also realized that I could use QQuickView rather than QQmlApplicationEngine to load my qml file (no idea what is the difference but I have tested the alternative code), though it did not help.

Do you have any of what is going on with my CMake project?

Additionally (and this is a secondary question): do you have any idea why I cannot write "qrc:/main.qml" when using the CMake project? In the .pro project I can use "qrc:/main.qml" and my qml files are neatly listed below the .qrc file in the project tree, which is not the case when using the CMake project.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
arennuit
  • 855
  • 1
  • 7
  • 23
  • 2
    1. Why do you need: `set(CMAKE_INCLUDE_CURRENT_DIR ON)`? 2. Why do you need: `set(CMAKE_AUTOMOC ON)`? 3. Instead of plenty `find_package` and `qt5_use_modules` try `find_package (Qt5Widgets REQUIRED)` and `target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Gui Qt5::OpenGL Qt5::QuickControls2)`. 4. Why do you need `include_directories(.)`? 5. `add_executable` must contain also compiled resources: `${RCC_SOURCES}`, etc... – Gluttton Jul 18 '16 at 12:16
  • Hi Glutton and thanks for your answer. You propose to replace replace the **qt5_use_modules** by **target_link_libraries** but isn't that more or less the same? Moreover you also need the many **find_package** to get **target_link_libraries** working, no? – arennuit Jul 18 '16 at 13:18
  • 1
    `but isn't that more or less the same?` more or less this is the same, but `qt5_use_modules` is deprecated. `Moreover you also need the many find_package`, IMO, no, only one (usually `Core` or `Widgets`). I hope my other [answer](http://stackoverflow.com/questions/25989448/implementing-qt-project-through-cmake/25990278#25990278) can be helpful . – Gluttton Jul 18 '16 at 13:52
  • 1
    If you didn't manage to get QRC working with CMake, then that is causing the problem. qtquickcontrols2.conf is only loaded from QRC. – jpnurmi Jul 18 '16 at 15:29
  • @jpnurmi: good point – arennuit Jul 19 '16 at 10:24
  • @Glutton: thanks a lot your answers were very helpful, things now work fine ;) – arennuit Jul 19 '16 at 10:25
  • You should get the file **qrc_qml.cpp** within your build folder. Does it exist? Are there two or three sections for **qtquickcontrols2.conf** within? – Th. Thielemann Jan 10 '17 at 13:14

1 Answers1

11

First just add this to your cmake file

find_package(Qt5 COMPONENTS Core Quick QuickControls2 REQUIRED)

than add this

target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick Qt5::QuickControls2)

in your CmakeLists.txt

Declan Nnadozie
  • 1,805
  • 1
  • 10
  • 20