2

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.

Saurbaum
  • 415
  • 1
  • 4
  • 22
  • As I mentioned in the comments of Victor's answer, you really should have gotten an error message for this. – Mitch Jan 30 '15 at 22:30
  • I definitely didn't get an error. That said I also have the Momentics IDE constantly telling me Q_OBJECT is a syntax error and no amount of ensuring the project is set up with the correct libs will convince it otherwise. – Saurbaum Feb 02 '15 at 08:59
  • 1
    To wrap up all the error reporting the reason I wasn't getting any errors is in Momentics to get QML errors you specifically have to run the application as QML Debug. The regular Debug target was hiding everything and only working on the C++ code. – Saurbaum Feb 02 '15 at 09:11

1 Answers1

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.

VP.
  • 15,509
  • 17
  • 91
  • 161
  • 3
    This is the correct answer, but the part about it calling a function is wrong. If you run OP's code, you'll get something like `TypeError: Property 'alarmCount' of object ApplicationUI(0x18f6a0) is not a function`. It's not a slot or invokable function, it doesn't exist. – Mitch Jan 30 '15 at 22:30
  • Did not have mistakes like this yet, but I agree with you: since it is not Q_INVOKABLE or public slot we can't have any access to it. Thanks for the pointing it out. But why did not @Saurbaum complained about the error message then..? – VP. Jan 31 '15 at 07:43
  • This was indeed the problem. No there were definitely no errors either at compile time or in the Console window at run time. – Saurbaum Feb 02 '15 at 08:52