c++qtqmlqquickwidget

Set QML theme using QQuickWidget


I have a QMainWindow application that shows a QWidget (FormOverview) that in turns embed a QQuickWidget. I'm not able to set the QML theme.

I created a "qtquickcontrols2.conf" and added it to qml.qrc:

[Controls]
Style=Material

[Material]
Theme=Dark
Accent=Orange
Primary=White

[Material\Font]
PixelSize=50

here my main.qml:

import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Controls.Material 2.3

Rectangle {
    id: root
    width: 1000
    height: 1000
    visible: true

    TabBar {
        id: bar
        width: root.width

        TabButton { text: qsTr("TAB 1") }
        TabButton { text: qsTr("TAB 2") }
        TabButton { text: qsTr("TAB 3") }
        TabButton { text: qsTr("TAB 4") }
    }
}

I set the main.qml resource file for the QQuickWidget from the Designer. Here the main.cpp code:

int main(int argc, char *argv[])
{   
    QApplication a(argc, argv);
    MainWindow w;
    w.showMaximized();

    return a.exec();
}

MainWindow.h FormOverview _formOverview;

MainWindow.cpp:

ui->tabView->addTab(&_formOverview, tr("Overview"));

I didn't set anything else in the code, because I thought it should find and apply the config file automatically. Instead, it renders the controls with the default theme.


Solution

  • As stated in the documentation, the QT_QUICK_CONTROLS_CONF variable is set to ":/qtquickcontrols2.conf" by default. Hence you have to be sure your qtquickcontrols2.conf is added to the resource file in the root prefix / and not in any other sub-directory.