Thanks wysota. I've attempted this approach (i.e., wrapping a QHash in a QObject-derived class), but the QML side does not seem to be updated when a value changes in the QHash data member. Here are some code snippets:
class BaseApplication
: public QObject{
Q_OBJECT
Q_PROPERTY(ParameterContainer* parameters READ parameters NOTIFY parametersChanged)
public:
explicit BaseApplication
(QObject *parent
= 0);
ParameterContainer* parameters() const;
signals:
void parametersChanged(ParameterContainer* arg);
private:
ParameterContainer* m_parameters;
};
class BaseApplication : public QObject
{
Q_OBJECT
Q_PROPERTY(ParameterContainer* parameters READ parameters NOTIFY parametersChanged)
public:
explicit BaseApplication(QObject *parent = 0);
ParameterContainer* parameters() const;
signals:
void parametersChanged(ParameterContainer* arg);
private:
ParameterContainer* m_parameters;
};
To copy to clipboard, switch view to plain text mode
When the m_parameters data member is updated in the .cpp file, I emit the parametersChanged signal to update QML, but it doesn't update.
class ParameterContainer
: public QObject{
Q_OBJECT
public:
explicit ParameterContainer
(QObject *parent
= 0);
void setParameterDisplayValue
(const QString name,
const QString value
);
Q_INVOKABLE
QString getParameterDisplayValue
(const QString name
) const;
private:
QHash<QString, QString> m_parameters;
};
class ParameterContainer : public QObject
{
Q_OBJECT
public:
explicit ParameterContainer(QObject *parent = 0);
void setParameterDisplayValue(const QString name, const QString value);
Q_INVOKABLE QString getParameterDisplayValue(const QString name) const;
private:
QHash<QString, QString> m_parameters;
};
To copy to clipboard, switch view to plain text mode
Assuming the parameters property of the BaseApplication class is exposed as a context property QML, the QML code looks like this:
Text
{
text: "Param value: " + "<b>" + _parameters.getParameterDisplayValue("param1") + "</b>"
}
Text
{
text: "Param value: " + "<b>" + _parameters.getParameterDisplayValue("param1") + "</b>"
}
To copy to clipboard, switch view to plain text mode
Is the QML side not being updated because I'm trying to bind to a Q_INVOKABLE rather than a Q_PROPERTY? If so, how could I work around this issue?
Thanks in advance!
Bookmarks