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
Highlighted
Contributor
Posts: 26
Registered: ‎08-13-2013
My Device: Developer
My Carrier: Developer
Accepted Solution

C++ function call

[ Edited ]

Hi,

I have two cpp files, applicationui.cpp and message.cpp

i want to call the function of message.cpp from main.qml, but there is some problem. Please help

 

 

applicationui.cpp

ApplicationUI::ApplicationUI(bb::cascades::Application *app) :
        QObject(app)
{
    // prepare the localization
    m_pTranslator = new QTranslator(this);
    m_pLocaleHandler = new LocaleHandler(this);
    if(!QObject::connect(m_pLocaleHandler, SIGNAL(systemLanguageChanged()), this, SLOT(onSystemLanguageChanged()))) {
        // This is an abnormal situation! Something went wrong!
        // Add own code to recover here
        qWarning() << "Recovering from a failed connect()";
    }
    // initial load
    onSystemLanguageChanged();

    // Create scene document from main.qml asset, the parent is set
    // to ensure the document gets destroyed properly at shut down.

    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("app",this);
    Message *msg = new Message();
    qml->setContextProperty("messageObj",msg);

    // Create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();

    // Set created root object as the application scene
    app->setScene(root);
}

 

in main.qml

 

 Button {
            text: "call function";
             onClicked: {
                 messageObj.myFunction();
            }
        }

 I have declared myFunction() as Q_INVOKABLE.

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: C++ function call

Hi! We need more information. Please post Message.h, Message.cpp contents and exact error messages the compiler is giving.
Or, if the app compiles and starts successfully, error messages which appear in runtime logs.

Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: C++ function call

You're better off attaching your Message class to the QML and calling it that way...

 

ApplicationUI::ApplicationUI(bb::cascades::Application *app)
	: QObject(app)
{
    qmlRegisterType<MessageClass>("custom.lib", 1, 0, "MessageClass");

 then in the QML,

 

Container {

    onCreationCompleted: {
        messageClass.callfunction();
    }

    attachedObjects: [
        MessageClass {
            id: messageClass
        }
    ]
}

 

 

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: C++ function call

[ Edited ]

Later on you can then make these attached classes delegates to make your QML more dynamic and therefore more efficient.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 828
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: C++ function call

since the message class is coming from c++ would it need to be delegated from c++ or will that work in qml

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: C++ function call

[ Edited ]

No delegates are specified in the QML...

 

http://developer.blackberry.com/native/reference/cascades/bb__cascades__qmlcontroldelegate.html


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Contributor
Posts: 26
Registered: ‎08-13-2013
My Device: Developer
My Carrier: Developer

Re: C++ function call

[ Edited ]

Hi BBSJDev,

I did what you suggested but i'm having the following errors in log :

 

o-g/.obj/applicationui.o: In function `int qmlRegisterType<Message>(char const*, int, int, char const*)':
c:/IDE/target_10_1_0_4828/qnx6/usr/include/qt4/QtDeclarative/qdeclarative.h:162: undefined reference to `Message::staticMetaObject'
c:/IDE/target_10_1_0_4828/qnx6/usr/include/qt4/QtDeclarative/qdeclarative.h:188: undefined reference to `Message::staticMetaObject'
o-g/.obj/applicationui.oSmiley Sad.rodata._ZTVN19QDeclarativePrivate19QDeclarativeElementI7MessageEE[_ZTVN19QDeclarativePrivate19QDeclarativeElementI7MessageEE]+0x8): undefined reference to `Message::metaObject() const'
o-g/.obj/applicationui.oSmiley Sad.rodata._ZTVN19QDeclarativePrivate19QDeclarativeElementI7MessageEE[_ZTVN19QDeclarativePrivate19QDeclarativeElementI7MessageEE]+0xc): undefined reference to `Message::qt_metacast(char const*)'
o-g/.obj/applicationui.oSmiley Sad.rodata._ZTVN19QDeclarativePrivate19QDeclarativeElementI7MessageEE[_ZTVN19QDeclarativePrivate19QDeclarativeElementI7MessageEE]+0x10): undefined reference to `Message::qt_metacall(QMetaObject::Call, int, void**)'
cc: C:/IDE/host_10_1_0_238/win32/x86/usr/bin/ntox86-ld caught signal 1
make[2]: *** [o-g/yproject] Error 1
make[1]: *** [debug] Error 2
make[2]: Leaving directory `C:/Users/KU5006815/Documents/BlackBerry10/yproject/x86'
make: *** [Simulator-Debug] Error 2
make[1]: Leaving directory `C:/Users/KU5006815/Documents/BlackBerry10/yproject/x86'

Contributor
Posts: 26
Registered: ‎08-13-2013
My Device: Developer
My Carrier: Developer

Re: C++ function call

Hi Zmey,

 

 

message.hpp


#ifndef MESSAGE_HPP_
#define MESSAGE_HPP_

#include<QObject>
#include "message.hpp"

class Message:public QObject
{
	Q_OBJECT
public:
	Message();
	Q_INVOKABLE void myFunction();
};


#endif /* MESSAGE_HPP_ */

 

 

message.cpp


/*
 * message.cpp
 *
 *  Created on: Sep 27, 2013
 *      Author: KU5006815
 */

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>

#include "message.hpp"

using namespace bb::cascades;


Message::Message()
{

}

void Message::myFunction()
{

	qDebug()<<"myFunction Called";
}

 

 

Error is : 

asset:///main.qml:19: TypeError: Result of expression 'messageObj.myFunction' [undefined] is not a function.

 

Contributor
Posts: 26
Registered: ‎08-13-2013
My Device: Developer
My Carrier: Developer

Re: C++ function call

Solved!
Thanks BBSJdev Smiley Happy
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: C++ function call

[ Edited ]

You're welcome and thanks for being my 100th marked solution. Smiley Happy

 

It's good when someone who has been helped, takes the time to do this.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.