Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QQuick with CMake: style does not work

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.

like image 632
arennuit Avatar asked Jul 18 '16 09:07

arennuit


1 Answers

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

like image 161
Declan Nnadozie Avatar answered Nov 03 '22 10:11

Declan Nnadozie