Solved Use Color / Font,FontSize parameterized tables
-
@Jehyeok Instead of setting it as a context property I would suggest you to create and register it as a singleton object as explained earlier. This way you can easily bind the properties from the singleton object. And thus on XML update from singleton the binded properties in your QML too gets updated.
-
@p3c0 Thank you. I got point. and it wokrs indeed.
I am able to get the singleton object (ThemeLoader) in qml using 'qmlRegisterSingletonType'.
ex, MYQML.qml
import ThemeLoader 1.0
Text {
color: ThemeLoader.colorText
}Two more questions,
- How I can get the singleton object and call the function in cpp code?
- How to notify the changes in MYQML.qml Text by emitting signals when ThemeLoader colorText changes? Also how do you emmit signals when QMap property changes?
-
How I can get the singleton object and call the function in cpp code?
Like this.
How to notify the changes in MYQML.qml Text by emitting signals when ThemeLoader colorText changes?
By using Q_PROPERTY. Add
NOTIFY
to allow QML bindings.Also how do you emmit signals when QMap property changes?
Perhaps in the same way ? Using datatype as
QMap
in Q_PROPERTY. But IMO instead of usingQMap
of properties I think its better to use individual properties so that the binding is executed per property. -
@p3c0
Thank you for quick reply.
I take your advice and I will use individual properties. But it doesn't work ...Please take a look
Theme.h
Q_PROPERTY(QColor color READ getColor WRITE setColor NOTIFY colorChanged)
public:
void setColor(QColor inputcolor)
{
m_color= inputcolor;
emit colorChanged();
}
QColor getColor() const
{
return m_color;
}
signals:
void colorChanged();
private:
QColor m_color;Theme.cpp
// init
setColor("red")// later to see changes, supposed to update color on text
setColor("blue")text.qml
Text {
id: text_
color: Theme.color -
@Jehyeok That's strange. Does the
Theme
class haveQ_OBJECT
macro ?
Can you share rest of the code ? When are you changing the color to new one. -
@p3c0 this is details
!!!! PS text color has been changed !! but the current top position dialog which triggered the change theme only.
Not main and existed other windows !! I would like to update colors in all windowHeader
class SPColors : public QObject
{
Q_OBJECT
Q_PROPERTY(QColor color READ getColor WRITE setColor NOTIFY colorChanged)
public:
SPColors();
virtual ~SPColors();Q_INVOKABLE QString getTheme() const; Q_INVOKABLE bool setTheme(const QString& theme); void setColor(QColor inputcolor) { color = inputcolor; emit colorChanged(); } QColor getColor() const { return color; } signals: void colorChanged(); private: QString currentTheme_; QColor color; };
CPP
SPColors::SPColors()
{
setColor(QColor("#FFFFFF"));
}setTheme(...String theme) // this is called by other dialog
{
if (theme=="white")
setColor(QColor("#000000"));
else
setColor(QColor("#FFFFFF"));
}QML
Text {
id: text_
color: SPColors.color -
!!!! PS text color has been changed !! but the current top position dialog only. Not main window !! I would like to update colors in all window
Good. Apply the same logic to other windows too.
-
@p3c0
Oh may you miss-understood.I set this color in the Text component which is used all my windows.
MyText.qml
Text
{
color: SPColors.color
}DialogA.qml
MyText
{
}
DialogB.qml
MyText
{
}I expected to see all text color changes (in dialogA, dialogB).
But it is applied only one dialog(DialogA which trigger setTheme and emit colorChanged).
Do you know how to apply it in entire windows? -
@Jehyeok It should work too. I tested a similar case and it works.
Do you know how to apply it in entire windows?
The way you tried should work.
-
@p3c0 You are right !!!
Beautiful !!