Qt 5样式:动态加载qml文件 [英] Qt 5 Styling: dynamically load qml files

查看:305
本文介绍了Qt 5样式:动态加载qml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试以其他样式扩展我们的应用程序。
对于每种样式,我都有一个单独的qml文件,例如颜色定义。
StyleA.qml:

I am currently trying to extend our application with different style. For each style I have a separate qml file with e.g. color definitions. StyleA.qml:

import QtQuick 2.0
QtObject {
    property color textColorStandard: 'black'
    ...
}

在我的main.qml中,我会喜欢根据软件属性加载正确的qml文件:

In my main.qml I would like to load the right qml file, based on a software property:

import QtQuick 2.0
Item {
    id: mainPanel
    ....

    Loader {
        id: myDynamicStyle
        source:  Property.UseThemeA? "StyleA.qml" : "StyleB.qml"
    }
    ...
    Item {
            id: BackGround
            color: myDynamicStyle.textColorStandard
    }
}

不幸的是,这种方法不起作用。还有其他/更好的方式来完成样式设置吗?

unfortunately this approach does not work. Is there any other/better way to accomplish styling?

谢谢,迈克尔

推荐答案

使用错误地加载到Loader中的东西,我宁愿:

Using things loaded in Loader is badly typed, I would rather :

首先,为我的所有样式创建一个共同的祖先组件,例如:

First, create a common ancestor Component for all my styles, e.g :

// AbstractStyle.qml
import QtQuick 2.0;
QtObject {
    property color textColorStandard;
}

接下来,派生它来创建自定义样式,例如:

Next, derivate it to create custom styles, e.g :

// StyleA.qml
import QtQuick 2.0;
AbstractStyle {
    textColorStandard: "blue";
}

// StyleB.qml
import QtQuick 2.0;
AbstractStyle {
    textColorStandard: "green";
}

然后在我的对象中使用必须使用样式的强类型属性,例如:

Then use a strongly typed property in my object that must use a style, e.g:

// main.qml
import QtQuick 2.0
Item {
    id: base;

    Component { id: compoStyleA; StyleA { } }
    Component { id: compoStyleB; StyleB { } }

    property AbstractStyle currentStyle : {
        var tmp = (Property.UseThemeA ? compoStyleA : compoStyleB); // choose component
        return tmp.createObject (base); // instanciate it and return it
    }

    Rectangle {
        color: currentStyle.textColorStandard;
    }
}

这样,有很多优点:


  1. 您的代码不使用字符串来标识组件,因此更容易发现和避免错误;

  2. 您不会影响不会继承样式基类的项目,因此不会遇到未定义的属性错误,也不需要进行鸭式打字来确定样式对象中包含哪些信息;

  3. 您没有Loader,因此不必在Loader的内部和外部之间遭受讨厌的上下文隔离;

  4. 所有组件都将在运行时预加载,以实例化的速度提高,并允许您从头开始查看样式中是否存在语法错误,而不是仅在更改当前样式时才能看到;

  5. 最后但并非最不重要的一点是,属性自动完成将在QtCreator中起作用,因为IDE会知道实际的类型!

  1. your code doesn't use Strings to identify components, so errors are easier to find and to avoid ;
  2. you can't affect an item that doesn't inherit your style base class, so you won't encounter "undefined property" errors and won't need ducktyping to figure out which information you have in your style object ;
  3. you don't have Loader so you won't have to suffer the nasty "context isolation" between inside and outside the Loader ;
  4. all components will be preloaded at runtime, gaining speed at instanciation, and allowing you to see from the start if there are syntax errors in the Styles, instead of seeing it only when changing current style ;
  5. last but not least, property auto-completion will work in QtCreator as the actual type will be known by the IDE !

这篇关于Qt 5样式:动态加载qml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆