QML binding not updating

99封情书 提交于 2019-12-11 04:58:36

问题


I have a simple BB10 app with a QML front end.

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();
            }
        }
    }
}

And the C++ class

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();
}

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.


回答1:


You did not actually make a binding to the variable. Correct binding will look like:

text: app.alarmCount

But in your code it is:

text: app.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.



来源:https://stackoverflow.com/questions/28239079/qml-binding-not-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!