简体   繁体   中英

QML binding not updating

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM