我正在尝试implement my own style,为此,我想覆盖内置对象。这就是我正在做的事情:
// main.cpp
QQuickStyle::setStyle("mystyle");
和
// mystyle/Button.qml
import QtQuick 2.5
import QtQuick.Controls 2.1 as Controls
Controls.Button {
background: Rectangle {
color: "green"
}
}
和
// qml.qrc
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>mystyle/CheckBox.qml</file>
</qresource>
</RCC>
根据文档,我认为这应该自动运作using file selectors。
然而,我的应用程序在启动时挂起。我的猜测是我陷入递归导入。我该如何正确地做到这一点?
答案 0 :(得分:4)
The Qt Quick Controls 2 styling system is based on QML type registration. When you run your app with mystyle, the type known as QtQuick.Controls.Button IS mystyle/Button.qml
. Therefore mystyle/Button.qml
cannot inherit QtQuick.Controls.Button. It cannot inherit itself.
This is basically the same as writing the following C++:
// button.h
#include "button.h"
class Button : public Button {};
A bit radicalized, but easy to understand analogy. :)
What you can do is to have (My)Button.qml
somewhere, let it inherit QtQuick.Controls.Button, and don't register it as a Qt Quick Controls 2 but simply import the folder. This is what we call as "Customizing Qt Quick Controls 2".
答案 1 :(得分:0)
在加载导入Qt Quick Control的QML之前必须配置样式。在注册QML类型后,无法更改样式。 setStyle()
在自定义样式的qml文件中应用现有样式。
注意:建议使用QQmlApplicationEngine,它在内部创建一个QQmlFileSelector实例。这就是使用QML文件选择器所需的全部内容。