Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
oddboy
Posts: 136
Registered: ‎05-24-2011
My Device: Z30 running 10.2 and Playbook 4GLTE running OS2.1
Accepted Solution

exposing C++ to QML

Hey guys,

 

I'm trying to expose a C++ class to my QML app.  I'm just doing some simple testing right now based on the examples here: https://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_qml/index.html

 

I have a simple class defined using the QOBJECT and QPROPERTY (no Q_INVOKABLE methods).

 

i'm not clear on one thing.  In the example, the class is defined as:

 

#ifndef MYCPPCLASS_H_
#define MYCPPCLASS_H_
 
#include <QObject>
#include <QMetaType>
 
class MyCppClass : public QObject {
    Q_OBJECT
    Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
 
public:
    MyCppClass();
    virtual ~MyCppClass();
 
    Q_INVOKABLE void reset();
 
    int value();
    void setValue(int i);
 
signals:
    void valueChanged(int);
 
private:
    int m_iValue;
};
 
#endif

 in the Q_PROPERTY line, there's a variable, a "read" method, a "write" method, and a signal to emit.

 

the variable isn't used anywhere.

 

the class definition looks like this:

 

#include "MyCppClass.h"
 
MyCppClass::MyCppClass()
{
    m_iValue = 0;
}
 
MyCppClass::~MyCppClass()
{
}
 
int MyCppClass::value()
{
    return m_iValue;
}
 
void MyCppClass::setValue(int i)
{
    m_iValue = i;
    emit valueChanged(m_iValue);
}
 
void MyCppClass::reset()
{
    m_iValue = 0;
    emit valueChanged(m_iValue);
}

Here's my test class.

 

#ifndef SIMPLECLASS_HPP_
#define SIMPLECLASS_HPP_
#include <QObject>
#include <QMetaType>

class SimpleClass : public QObject{
	Q_OBJECT
	Q_PROPERTY(int iInt READ iGetInt WRITE iSetInt NOTIFY intChanged)

public:
	SimpleClass();
	virtual ~SimpleClass();
	int iGetInt();
	void iSetInt(int i);

signals:
	void intChanged(int);

private:
	// private variables
	int myInt;
};


#endif /* SIMPLECLASS_HPP_ */

I *think* the iSetInt(int) is working, but the iGetInt() is not.

 

I'm trying to do this in my QML:

 

 Button {
                id: myButton
                text: "action"
                onClicked: {
                                if (myLabel.text == "you pushed it!")
                                {
                                    myLabel.text = "you pushed it again!";
                                }
                                else {
                                    myLabel.text = "you pushed it!";
                                }
                                mySimpleClass.iSetInt(44);
                                myOtherLabel.text = "onClicked: " + mySimpleClass.iGetInt();
                            } // onClicked
            } // Button

 any ideas what i'm doing wrong?

 

 

bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com
Developer
Dredvard
Posts: 160
Registered: ‎01-27-2012
My Device: Playbook

Re: exposing C++ to QML

In there example, value is the parameter that should be accessible from QML.  setValue() and Value() are the function that QML calls whenver a read or write occurs.  I'm not clear what's wrong with your cde, but try accessing value directly instead of using the functions - or in your case you iInt.

 

 

---
If you find my post helpful please "like" it and "accept as a solution"
Developer
oddboy
Posts: 136
Registered: ‎05-24-2011
My Device: Z30 running 10.2 and Playbook 4GLTE running OS2.1

Re: exposing C++ to QML

Yes, thank you.

 

i found that calling the functions directly isn't how this works.

 

rather than: 

 

myClass.iSetValue(101);  // doesn't work

 

i tried this:

 

myClass.iInt = 101; //works

 

...even easier than i thought!

 

bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com