QML绑定未更新 [英] QML binding not updating
问题描述
我有一个带有QML前端的简单BB10应用程序.
I have a simple BB10 app with a QML front end.
GUI由几个按钮和一个标签组成
The GUI consists of a couple of buttons and a label
Page {
Container {
Label {
text: app.alarmCount()
}
Button {
text: qsTr("Resend Notification")
onClicked: {
app.resendNotification();
}
}
Button {
text: qsTr("Stop Service")
onClicked: {
app.stopService();
}
}
Button {
text: qsTr("Kill Service")
onClicked: {
app.killService();
}
}
}
}
还有C ++类
class ApplicationUI: public QObject
{
Q_OBJECT
Q_PROPERTY(QString alarmCount READ alarmCount NOTIFY AlarmUpdate)
public:
ApplicationUI();
virtual ~ApplicationUI() { }
Q_INVOKABLE void resendNotification();
Q_INVOKABLE void stopService();
Q_INVOKABLE void killService();
QString alarmCount() const;
void setAlamCount(int newCount);
signals:
void AlarmUpdate();
private:
bb::system::InvokeManager* m_invokeManager;
QString m_alarmCountDisplay;
};
和班级中希望相关的部分
and the hopefully relevant bit of the class
QString ApplicationUI::alarmCount() const
{
return m_alarmCountDisplay;
}
void ApplicationUI::setAlamCount(int newCount)
{
m_alarmCountDisplay = QString("%1 Alarms").arg(newCount);
emit AlarmUpdate();
}
我的问题是标签从不显示警报计数字符串属性.我在emit上设置了一个断点,可以看到它正在被调用,在alarmCount()getter上可以看到,并返回正确的值,但是前端实际上从未显示标签的值.
My problem is the label never displays the alarm count string property. I have set a breakpoint on the emit and can see it's getting called and on the alarmCount() getter and can see that's returning the correct value but my front end never actually shows a value for the label.
推荐答案
您实际上并未对该变量进行绑定.正确的绑定如下所示:
You did not actually make a binding to the variable. Correct binding will look like:
text: app.alarmCount
但是在您的代码中是:
text: app.alarmCount()
使用您的代码会出错,因为您无法访问Q_OBJECT
或public slot
以外的任何Q_OBJECT
方法.但是,即使您对方法进行了标记,这也意味着您仅一次获得了alarmCount属性,并且由于您未进行绑定而仅进行了一个方法调用,因此不会更新该属性.
With your code it makes an error because you can't access any method of Q_OBJECT
which is not Q_INVOKABLE
or public slot
. But even if you make such mark to your methods it means that you get alarmCount property only one single time and it will not be updated since you did not make a binding but just one method call.
这篇关于QML绑定未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!