[Interest] Creating a custom QML style with Material as a fallback, but with a dark theme

EXT Mitch Curtis mitch.curtis at qt.io
Wed Sep 20 03:44:00 CEST 2023


Hi Matthew,

> -----Original Message-----
> From: Interest <interest-bounces at qt-project.org> On Behalf Of Matthew
> Fincham via Interest
> Sent: Tuesday, September 19, 2023 1:13 PM
> To: interest at qt-project.org
> Subject: Re: [Interest] Creating a custom QML style with Material as a fallback,
> but with a dark theme
>
> Hi
>
> Following up on my own post.
>
> QQuickMaterialStyle logs a warning if an invalid theme is used (not 'Light' or
> 'Dark'). This is in the function that initialises the style's global variables. (in
> qquickmaterialstyle.cpp)
>
> When the 'Material' style is used as the fallback style, this log is not seen. This
> initialisation function is called from the 'Material' plugin (
> qtquickcontrols2materialstyleplugin.cpp), which seems to indicate the plugin's
> initialisation is not run when it is the fallback theme.
>
> Does this make sense, and is there a way to force the initialisation?

I'm looking into it, but as a workaround, assuming your style imports the Material style as a fallback in its qmldir (or IMPORTS in qt_add_qml_module):

    import QtQuick.Controls.Material auto

you can then use the attached API to set the theme:

    Material.theme: Material.Dark

> Thanks again.
> Matthew Fincham
>
> On 2023/09/18 12:52, Matthew Fincham via Interest wrote:
> > Hi
> >
> > I am attempting to create a custom QML style using this documentation:
> >
> https://doc/.
> > qt.io%2Fqt-6%2Fqtquickcontrols-customize.html%23definition-of-a-style&
> >
> data=05%7C01%7Cmitch.curtis%40qt.io%7C4e7b0a9253b649740eed08dbb
> 8cf6fb0
> >
> %7C20d0b167794d448a9d01aaeccc1124ac%7C0%7C0%7C638306973314
> 152407%7CUnk
> >
> nown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6I
> k1haWw
> >
> iLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=V2b3M0MBvsarYVwtDQd
> aF3kwWtc2xurp
> > AuA%2F10IABak%3D&reserved=0
> >
> > I won't provide sample code because it is close to line-for-line. The
> > custom style works - there is a custom button and the other controls
> > use the Material style. However, I can't get the Material style to run
> > with the Dark theme.
> >
> > The theme is set with an environment variable:
> >
> > QT_QUICK_CONTROLS_MATERIAL_THEME=Dark
> >
> > The style is set in C++:
> >
> > QQuickStyle::setStyle("MyStyle");
> >
> > If the style is "Material" this works as expected. With 'MyStyle',
> > that falls back to Material, a Light theme is used. Is there any was
> > to force the fallback style to use the Dark theme?
> >
> > Many thanks
> > Matthew Fincham
> > _______________________________________________
> > Interest mailing list
> > Interest at qt-project.org
> > https://list/
> > s.qt-
> project.org%2Flistinfo%2Finterest&data=05%7C01%7Cmitch.curtis%40q
> >
> t.io%7C4e7b0a9253b649740eed08dbb8cf6fb0%7C20d0b167794d448a9d0
> 1aaeccc11
> >
> 24ac%7C0%7C0%7C638306973314152407%7CUnknown%7CTWFpbGZsb3
> d8eyJWIjoiMC4w
> >
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7
> C%7C%7C
> >
> &sdata=%2F9LFtD8jWWX0qNZxQOv0ENQP3JuVbDEOgHDpIZ0R%2FoE%3D&
> reserved=0
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.q/
> t-
> project.org%2Flistinfo%2Finterest&data=05%7C01%7Cmitch.curtis%40qt.io
> %7C4e7b0a9253b649740eed08dbb8cf6fb0%7C20d0b167794d448a9d01aa
> eccc1124ac%7C0%7C0%7C638306973314152407%7CUnknown%7CTWFpb
> GZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI
> 6Mn0%3D%7C3000%7C%7C%7C&sdata=%2F9LFtD8jWWX0qNZxQOv0ENQ
> P3JuVbDEOgHDpIZ0R%2FoE%3D&reserved=0


More information about the Interest mailing list