Evgenii Legotckoi
Evgenii LegotckoiMarch 5, 2018, 3:13 a.m.

Android. Java vs Qt QML - Tutorial 000. Enable Material Design

When you develop both Java and Qt QML, you need to enable Material Design.

Material Design on Java

In the case of Java, it's enough to include the theme design in the styles.xml file and set the required color gamut for the application.

styles.xml

<resources>

    <style name="AppTheme" parent="android:Theme.Material">
        <!-- Main theme colors -->
        <!--   your app branding color for the app bar -->
        <item name="android:colorPrimary">@color/colorPrimary</item>
        <!--   darker variant for the status bar and contextual app bars -->
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
        <!--   theme UI controls like checkboxes and text fields -->
        <item name="android:colorAccent">@color/colorAccent</item>
    </style>

</resources>

colors.xml

Colors are specified in this file.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

Material Design on Qt QML

In order to use Material Design on Qt QML, you must enable the use of Qt Quick Controls 2 in the project profile.

QT += quick quickcontrols2

Next, we connect the QQuickStyle class in main.cpp to use the static method of this class to include the Material Design style for the entire application.

#include <QGuiApplication>
#include <QQuickStyle> // Class to enabling styling
#include <QQmlApplicationEngine>

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

    QGuiApplication app(argc, argv);

    QQuickStyle::setStyle("Material"); // Enable Material Design

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

Changing the color scheme

To change the color style of the style, use the configuration file qtquickcontrols2.conf , which should be placed in the project root next to the project profile. You can add this file to resources to make it easier to work with this file from the IDE.

[Material]
Primary=#3F51B5
Accent=#FF4081
Theme=#303F9F
Foreground=#ffff00
Background=#00ffff

Color scheme settings

In Java, there are the following color scheme settings

  • colorPrimary - basic color, for example color Action Bar
  • colorPrimaryDark - color of the status bar
  • textColorPrimary - color of text for inscriptions on top of elements with a background colorPrimary
  • windowBackground - application background color
  • navigationBarColor - color of the phone's navigation menu
  • colorAccent - color of elements, such as checkboxes, etc.

Qt QML has only five color settings

  • Primary - basic color, elements similar to Action Bar
  • Accent - color elements such as checkboxes
  • Theme - color theme, applied to both the main application window as a background, and for pop-up menus or other other elements. Can also be overridden by Foreground and Background properties
  • Foreground - a separate color theme setting, can be applied to text, redefines the Theme settings.
  • Background - It is used to define the background of such elements as the menu, Navigation Drawer, etc. redefines the settings of Theme.

For Theme, you can use System as the value, which means using system color palettes. But you can do color correction with Foreground and Background.

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

A
  • July 2, 2020, 5:05 p.m.

Подскажите пожалуйста, эта тема все еще актуальна? Или есть другие способы работать с темой? Вообщем хочу изменить цвет статус бара и бара навигации в приложении, но не знаю как это делать правильно. Можно сделать полный экран, но это кажется совсем не то...

Evgenii Legotckoi
  • July 3, 2020, 1:29 a.m.

Это актуально для изменения цвета. В файле qtquickcontrols2.conf переменная Primary должна влиять на цвет приложения соответственно и цвет ApplicationBar должен поменяться. Но у status bar вроде как нужно запускать код в Java потоке Android приложения.

Вот есть такое решение

QtAndroid::runOnAndroidThread([=]()
{
    QAndroidJniObject window = QtAndroid::androidActivity().callObjectMethod("getWindow", "()Landroid/view/Window;");
    window.callMethod<void>("addFlags", "(I)V", 0x80000000);
    window.callMethod<void>("clearFlags", "(I)V", 0x04000000);
    window.callMethod<void>("setStatusBarColor", "(I)V", 0xffffffff); // Desired statusbar color
});

Для изменения цвета текста

QAndroidJniObject decorView = window.callObjectMethod("getDecorView", "()Landroid/view/View;");
decorView.callMethod<void>("setSystemUiVisibility", "(I)V", 0x00002000);

Есть ещё вот такой репозиторий, который должен с этим работать github . Но насколько он актуален, я не знаю.
Но думаю, что лучше подключите проект из github в своём проекте. Он выглядит достаточно адекватным на беглый взгляд.

Comments

Only authorized users can post comments.
Please, Log in or Sign up
AD

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:80points,
  • Rating points4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:20points,
  • Rating points-10
Last comments
i
innorwallNov. 14, 2024, 12:07 p.m.
Circuit switching and packet data transmission networks Angioedema 1 priligy dapoxetine
i
innorwallNov. 14, 2024, 11:42 a.m.
How to Copy Files in Linux If only females relatives with DZ offspring were considered these percentages were 23 order priligy online uk
i
innorwallNov. 14, 2024, 9:09 a.m.
Qt/C++ - Tutorial 068. Hello World using the CMAKE build system in CLion ditropan pristiq dosing With the Yankees leading, 4 3, Rivera jogged in from the bullpen to a standing ovation as he prepared for his final appearance in Chicago buy priligy pakistan
i
innorwallNov. 14, 2024, 4:05 a.m.
EVILEG-CORE. Using Google reCAPTCHA 2001; 98 29 34 priligy buy
i
innorwallNov. 14, 2024, 4 a.m.
PyQt5 - Lesson 007. Works with QML QtQuick (Signals and slots) priligy 30mg Am J Obstet Gynecol 171 1488 505
Now discuss on the forum
i
innorwallNov. 14, 2024, 3:39 a.m.
добавить qlineseries в функции priligy amazon canada 93 GREB1 protein GREB1 AB011147 6
i
innorwallNov. 11, 2024, 10:55 a.m.
Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
9
9AnonimOct. 25, 2024, 9:10 a.m.
Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

Follow us in social networks