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
Posts: 31
Registered: ‎05-18-2014
My Device: z10

setContextProperty usage

Hi all.

 

I am using the below code to setcontentproperty

---------------------------------------------------------------

somecalssname(QObject *parent) {

 

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

qml->setContextProperty("_project", this);

 

qml->setContextProperty("_messages", newMessages(this));

-----------------------------------------------------------------

 

if i access  _project in qml file it  is working, but if i access _messages , it return "variable not found"  => this issue is happening only when i install first the app in device, if i reinstall the app again in device using Mometics, the  "_message" variable is accessible 

 

Not sure what is the issue,  i need help in this,

 

If you need more details about the code plz let me know....

 

Thank s in advance..........

 

 

Developer
Posts: 47
Registered: ‎11-23-2010
My Device: Q10

Re: setContextProperty usage

[ Edited ]

that line

 

qml->setContextProperty("_messages", newMessages(this));

 is it

new Messages(this)

 or

newMessages(this)

 ?

I have to admit, what happening sounds really strange, it would be better if you had the issue always, but since it disappears after reinstall, it really puzzles :smileyhappy:

 

May be more code can lit some light...

 

After more thinking, can I ask you to try replace

qml->setContextProperty("_messages", newMessages(this));

 by

m_somepointer = newMessages(this);
qml->setContextProperty("_messages", somepointer);

 where m_somepointer is the member of the class where all setContext happening (probably ApplicationUI).

Developer
Posts: 31
Registered: ‎05-18-2014
My Device: z10

Re: setContextProperty usage

Hi ,

 

Thanks for the reply.....

 

Here is the full code describtion plz go through it...

main.cpp
----------------------------------------------------- 

#include<bb/cascades/AbstractPane>

#include<bb/cascades/Application>

#include<bb/cascades/QmlDocument>

#include<bb/cascades/Application>

#include<bb/multimedia/AudioRecorder>

#include<bb/cascades/pickers/FilePicker>

#include<QLocale>

#include<QTranslator>

#include"applicationui.hpp"

#include<Qt/qdeclarativedebug.h>

#include"WeatherService.hpp"

#include"TimerCount.hpp"

#include<bb/pim/message/MessageUpdate>

#include<bb/system/phone/Phone>

#include<bb/data/DataSource>

#include"MOLT.hpp"

Application app(argc, argv);
	     // localization support
	     QTranslator translator;
	     QString locale_string = QLocale().name();
	     qDebug() << "tlocal string---------------test= >" << locale_string;
	     QString filename = QString( "Alameen_%1" ).arg( locale_string );
	     if (translator.load(filename, "app/native/qm")) {
	         app.installTranslator( &translator );
	     }
	     MOLT mMolt;
	     mMolt.initLocalization(&translator);

	     QObject::connect(&app, SIGNAL( thumbnail() ), &mMol, SLOT( onThumbnail() ));
	     QObject::connect(&app, SIGNAL( fullscreen() ),&mMol, SLOT( onFullscreen() ));

 

AND in the MOLT cpp file i am calling this

#include "MOLT.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include <bb/UIToolkitSupport>
#include <bb/data/DataSource>
#include <bb/cascades/maps/MapView>
#include <bb/cascades/maps/MapData>
#include <bb/cascades/maps/DataProvider>
#include <bb/platform/geo/Point>
#include <bb/platform/geo/GeoLocation>
#include <bb/platform/geo/Marker>
#include <bb/system/InvokeAction>
#include <bb/system/InvokeQueryTargetsReply>
#include <bb/system/InvokeQueryTargetsRequest>
#include <bb/system/InvokeReply>
#include <bb/system/InvokeRequest>
#include <bb/system/InvokeTarget>
#include <bb/system/InvokeTargetReply>
using namespace bb::cascades;
using namespace QtMobilitySubset;
using namespace bb::data;
using namespace bb::cascades::maps;
using namespace bb::platform::geo;

//myButtons = new Array(new Array(), new Array());

MOLT::MOLT(QObject *parent) {

	QmlDocument *qml = 	QmlDocument::create("asset:///main.qml").parent(this);

	qml->setContextProperty("_MOL", this);


	qml->setContextProperty("_messages", new Messages(this));

	qml->setContextProperty("_socialInvocation", new SocialInvocation());

	QString workingDir = QDir::currentPath();

	QDeclarativePropertyMap* dirPaths = new QDeclarativePropertyMap;
	dirPaths->insert("voice", QVariant(QString("file://" + workingDir + "/shared/voice/")));
	qml->setContextProperty("dirPaths", dirPaths);

	
	// create root object for the UI
	root = qml->createRootObject<AbstractPane>();
	// set created root object as a scene
	Application::instance()->setScene(root);

	 mInvokeManager = new InvokeManager(this);

	countPhoto =0;
	initNetworkConnection();
	startGPS();
}

 Here is MOLT.hpp file

#include <QObject>
#include <bb/cascades/Application>
#include <bb/cascades/LocaleHandler>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <QtCore/QObject>
#include <QtLocationSubset/qgeopositioninfo.h>
#include <QtLocationSubset/qgeopositioninfosource.h>
#include <bb/platform/geo/GeoLocation.hpp>
#include <QtLocationSubset/QGeoCoordinate>
#include <bps/geolocation.h>
#include <QPointer>
#include <bb/system/InvokeManager.hpp>
#include <bb/system/InvokeRequest.hpp>
#include <QtCore/qmap.h>
#include <bb/cascades/maps/MapView>
#include <bb/cascades/Button>
#include <bb/cascades/Container>
#include <bb/cascades/multimedia/CameraSettings>
#include "Messages.hpp"
#include "MessageComposer.hpp"
#include "MessageViewer.hpp"
#include "SocialInvocation.hpp"
#include "SMSexample.hpp"
#include "MapViewDemo.hpp"
using namespace QtMobilitySubset;
using namespace bb::system;
using namespace bb::cascades;
namespace bb {
namespace cascades {
class Application;
namespace maps {
class MapView;
}
}
namespace platform {
namespace geo {
class GeoLocation;
}
}
}

/*!
 * @brief Application pane object
 *
 *Use this object to create and init app UI, to create context objects, to register the new meta types etc.
 */
class MOLT : public QObject
{
    Q_OBJECT

public:
    MOL(QObject *parent = 0);

 Message.cpp and hpp file

message.cpp file

#include "Messages.hpp"
#include <bb/cascades/DropDown>
#include <bb/cascades/Option>
#include <bb/pim/account/AccountService>
#include <bb/pim/account/Account>
#include <bb/pim/account/Provider>
#include <bb/pim/message/MessageSearchFilter>
#include "MessageComposer.hpp"
#include "MessageViewer.hpp"
using namespace bb::cascades;
using namespace bb::pim::account;
using namespace bb::pim::message;

//! [0]
Messages::Messages(QObject *parent)
    : QObject(parent)
    , m_messageService(new MessageService(this))
    , m_model(new GroupDataModel(this))
    , m_messageViewer(new MessageViewer(m_messageService, this))
    , m_messageComposer(new MessageComposer(m_messageService, this))
    , m_currentMessageId(-1)
{
    // Disable grouping in data model
    m_model->setGrouping(ItemGrouping::None);

    // Ensure to invoke the filterMessages() method whenever a message has been added, changed or removed
    bool ok = connect(m_messageService, SIGNAL(messagesAdded(bb::pim::account::AccountKey, QList<bb::pim::message::ConversationKey>, QList<bb::pim::message::MessageKey>)), SLOT(filterMessages()));
    Q_ASSERT(ok);
    ok = connect(m_messageService, SIGNAL(messageAdded(bb::pim::account::AccountKey, bb::pim::message::ConversationKey, bb::pim::message::MessageKey)), SLOT(filterMessages()));
    Q_ASSERT(ok);
    ok = connect(m_messageService, SIGNAL(messageUpdated(bb::pim::account::AccountKey, bb::pim::message::ConversationKey, bb::pim::message::MessageKey, bb::pim::message::MessageUpdate)), SLOT(filterMessages()));
    Q_ASSERT(ok);
    ok = connect(m_messageService, SIGNAL(messageRemoved(bb::pim::account::AccountKey, bb::pim::message::ConversationKey, bb::pim::message::MessageKey, QString)), SLOT(filterMessages()));
    Q_ASSERT(ok);

    // Initialize the current account if there is any
    m_accountList = AccountService().accounts(Service::Messages);
    if(!m_accountList.isEmpty())
        m_currentAccount = m_accountList.first();

    // Fill the data model with messages initially
    filterMessages();
}

 message.hpp file

#include <bb/cascades/GroupDataModel>
#include <bb/pim/account/Account>
#include <bb/pim/message/MessageService>
#include <QtCore/QObject>

class MessageComposer;
class MessageViewer;

namespace bb {
namespace cascades {
class Option;
}
}

/**
 * @short The controller class that makes access to messages available to the UI.
 */
//! [0]
class Messages : public QObject
{
    Q_OBJECT

    // The model that provides the filtered list of messages
    Q_PROPERTY(bb::cascades::GroupDataModel *model READ model CONSTANT);

    // The pattern to filter the list of messages
    Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged);

    // The viewer object for the current message
    Q_PROPERTY(MessageViewer* messageViewer READ messageViewer CONSTANT);

    // The composer object for the current message
    Q_PROPERTY(MessageComposer* messageComposer READ messageComposer CONSTANT);

 i got stuck in this.... and also i dont understand the pointer way u said

"

m_somepointer = newMessages(this);
qml->setContextProperty("_messages", somepointer);
Developer
Posts: 47
Registered: ‎11-23-2010
My Device: Q10

Re: setContextProperty usage

[ Edited ]

It's a lot of code, but still not enough :smileyhappy:, may it was cutted when you paste it, for message.hpp I can see up to this line:

Q_PROPERTY(MessageComposer* messageComposer READ messageComposer CONSTANT);

 

 

What I also want to see is message.hpp with the part where you declare these members:

m_messageService
m_model
m_messageViewer
m_messageComposer
m_currentMessageId

 

May be try to use sourcepod.com to share big amount of code...

 

Now, back to what I was saying in previous email.

 

Here:

	qml->setContextProperty("_messages", new Messages(this));

Can you add in MOLT class new member:

Messages* m_pMessagesObject;

 And in constructor use something like this:

 

 

m_pMessagesObject = new Messages(this);
qml->setContextProperty("_messages", m_pMessagesObject);

 This is what I meant yesterday.

Also this:

qml->setContextProperty("_socialInvocation", new SocialInvocation());

 You don't have pointer to SocialInvocation, and it doesn't have QObject parent, means this thing will never be properly deleted. I'm NOT saying this is where the problem is, but this is just not right and its better to fix it

 

 

ttyl

 

Developer
Posts: 31
Registered: ‎05-18-2014
My Device: z10

Re: setContextProperty usage

Hi ,

 

thanks for the reply...

 

i used this but no use 

m_pMessagesObject = newMessages(this);

 

qml->setContextProperty("_messages", m_pMessagesObject);

 

what ever i assinged using  setContextProperty("_messages"m_pMessagesObject);  is not working, only this line is working 

qml->setContextProperty("_MOLT", this);

 

I posting another cpp file which i used for setContextProperty.. plz check...

 

this is used in MOLT.cpp file 

qml->setContextProperty("_smsexample",  new SMSexample(this));

 

ANd below is the code of smsexample.cpp and .hpp file

smsexaple.hpp
---------------------
// Default empty project template
#ifndef SMSexample_HPP_
#define SMSexample_HPP_

#include <QObject>
#include <QtCore/QtCore>
#include <QString>
#include <bb/cascades/Application>

namespace bb { namespace cascades { class Application; }}

/*!
 * @brief Application pane object
 *
 *Use this object to create and init app UI, to create context objects, to register the new meta types etc.
 */
class SMSexample : public QObject
{
    Q_OBJECT
public:
    SMSexample(QObject *parent = 0);
   // virtual ~SMSexample() {}

	Q_INVOKABLE void sendSMS(QString regID, QString contactNumber);
	Q_INVOKABLE void showSuccessMessage();

};


#endif /* SMSexample_HPP_ */

 

SMSexample.cpp
-----------------------
// Default empty project template
#include "SMSexample.hpp"

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

#include <bb/system/SystemToast>

#include <bb/pim/message/ConversationBuilder>
#include <bb/pim/message/MessageBuilder>
#include <bb/pim/message/MessageService>
#include <bb/pim/message/MessageContact>
#include <bb/pim/account/AccountService>

#include <bb/cascades/Page>
#include <QSettings>

using namespace bb::cascades;
using namespace bb::pim::account;

SMSexample::SMSexample(QObject *parent)
: QObject(parent){

}

// Following lines are for SMS sending (all of them)
void SMSexample::sendSMS(QString regID, QString contactNumber) {
	bb::pim::message::MessageService messageService;
	bb::pim::account::AccountService accountService;

	//Get the SMS/MMS account
	QList<Account> accountList = accountService.accounts(Service::Messages,"sms-mms");
	AccountKey accountId = accountList.first().id();

	// Create a contact to whom you want to send sms/mms.
	int contactKey = -1;
	bb::pim::message::MessageContact recipient = bb::pim::message::MessageContact(contactKey,bb::pim::message::MessageContact::To,contactNumber,contactNumber);

	//Create a conversation  because sms/mms chats most of the time is a conversation
    bb::pim::message::ConversationBuilder* conversationBuilder = bb::pim::message::ConversationBuilder::create();
    conversationBuilder->accountId(accountId);
    QList<bb::pim::message::MessageContact>participants;
    participants.append(recipient);
    conversationBuilder->participants(participants);
    bb::pim::message::Conversation conversation = *conversationBuilder;
    bb::pim::message::ConversationKey conversationId = messageService.save(accountId,conversation);

	//Create a message Builder for sms/mms
	bb::pim::message::MessageBuilder* messageBuilder = bb::pim::message::MessageBuilder::create(accountId);
	messageBuilder->addRecipient(recipient);
	// SMS API's handle body as an attachment.
	QString body = regID;
	messageBuilder->addAttachment(bb::pim::message::Attachment("text/plain", "body.txt", body));
	messageBuilder->conversationId(conversationId);
	bb::pim::message::Message message;
	message = *messageBuilder;

	//Sending SMS/MMS
	//messageService.send(accountId, message);
	bb::pim::message::MessageKey key = messageService.send(accountId, message);

	showSuccessMessage();
}

void SMSexample::showSuccessMessage() {
	using namespace bb::system;

	SystemToast *toast = new SystemToast(this);

    toast->setBody("SMS sending success!");
    toast->show();
}

 waiting for ur needful reply ..... thanks in advance.......

 

Developer
Posts: 47
Registered: ‎11-23-2010
My Device: Q10

Re: setContextProperty usage

I've created test project, with additional 3 different classes, each has Q_INVOKABLE methods to access members. I set context properties for objects of each of them, including this. Basically I tried to reproduce your scenario. I couldn't.

 

Even so you posted a lot of source code, I don't see the part where the problem you described can happen. So here's my suggestion: Comment most of the code until you get things work right. Then start uncommenting and run each time after that until you can reproduce the issue. It's long process but helps quite often.

 

Developer
Posts: 31
Registered: ‎05-18-2014
My Device: z10

Re: setContextProperty usage

HI ,

 

 

when i first install the app thorught momentics , i am getting this error

 

"

Error in final launch sequence

Cannot prepare and download the binary

Cannot locate the application process. The application may not have launched successfully. Please check the device for pop-up dialogs or log files related to this issue."

 

and when i again install the app using momentics , it successfully installs,,,,

 

is this creating any problem?

Developer
Posts: 47
Registered: ‎11-23-2010
My Device: Q10

Re: setContextProperty usage

Never seen that errors before. Are you connecting the device through usb or wifi? If wifi then try usb and vice versa. Anyway I think those errors in your post are your real problem, not the setContextProperty.

 

I would recommend you to create new topic with another name. Current seems little bit irrelevant.