Я смог заставить его работать, благодаря другим темам.
Прежде всего, создайте свои темы, как этот пользователь, которые наследуют от AbstractStyle
, что обеспечивает гораздо большую гибкость.
https://stackoverflow.com/a/25866188/2425044
Наши property
будет определяться значением, возвращаемым функцией JS
:
import "qrc:/graphics/componentCreation.js" as Theme
Item
{
id: homeViewItem
anchors.centerIn: parent
// Load default theme at startup
property AbstractTheme currentTheme: Theme.createThemeObject(homeViewItem, "qrc:/redTheme/redTheme.qml");
Rectangle
{
color: currentTheme.textColorStandard;
}
}
componentCreation.js
// Create themes components and load them in the apps' QML files
var component;
var sprite;
function createThemeObject(item, themePath)
{
component = Qt.createComponent(themePath);
sprite = component.createObject(item);
if (sprite === null)
console.log("componentCreation.js: error creating " + themePath + " object");
else
return sprite;
}
Допустим, вы хотите изменить тему когда пользователь нажимает на Button
:
Button
{
id: themeButton
text: "Change to blue theme"
onClicked:
{
// Remove content of redTheme.rcc, register blueTheme.rcc
cpp_class.changeTheme("redTheme", "blueTheme")
// blueTheme's content is now available, let's fill its content into a QML object
currentTheme = Theme.createThemeObject(homeViewItem, "qrc:/blueTheme/blueTheme.qml")
}
}
Помните, что redTheme.qml и blueTheme.qml содержатся в qrc
файлах, которые сами скомпилированы в rcc
файлы.
Вот определение changeTheme(const QString&, const QString&)
, который отменяет регистрацию старую тему и регистрирует новый:
void cpp_class::changeTheme(const QString &oldTheme, const QString &newTheme)
{
bool un = QResource::unregisterResource(QCoreApplication::applicationDirPath() + "/data/themes/" + app + "/" + oldTheme + ".rcc");
if (!un)
std::cerr << oldTheme.toStdString() << "could not be unregistered" << std::endl;
bool in = QResource::registerResource(QCoreApplication::applicationDirPath() + "/data/themes/" + app + "/" + newTheme + ".rcc");
if (!in)
std::cerr << newTheme.toStdString() << "could not be registered as an external binary resource" << std::endl;
}
Другие темы, которые помогли мне:
https://wiki.qt.io/Qml_Styling
http://www.slideshare.net/BurkhardStubert/practical-qml-key-navigation (начинается на слайде 34)