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: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

send a signal via c++ to qml

import bb.cascades 1.0
import com.zain.tools 1.0

NavigationPane {
   
    id: nav
    objectName: "myNavi"
    onCreationCompleted: {
        Application.menuEnabled = false;
        //timer.start()
    }

    Splash {
        id: splash
    }
    attachedObjects: [
        Dialog {
            id: myDialog
            
            Container {
                horizontalAlignment: HorizontalAlignment.Fill
                verticalAlignment: VerticalAlignment.Fill
                
                Button {
                    horizontalAlignment: HorizontalAlignment.Center
                    text: "Hide Dialog"
                    onClicked: myDialog.close()
                }
            }
        },
        MyTimer {
            id: timer
            interval: 5000
            onTimeout: {

                nav.remove(splash)
                //nav.push(main.createObject())
                stop()
            }
        },
        ComponentDefinition {
            id: main
            source: "ServerSelection.qml"
        },
        ImagePaintDefinition {
            id: back
            repeatPattern: RepeatPattern.XY
            image: tracker.image
        },
        ImageTracker {
            id: tracker
        }
    ]

}

 

 

I want on emit authorizationFailure(); a signal defined, to do mydialog.open

how can i send a signal from my c++ to qml?

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: send a signal via c++ to qml

am getting

 

Multiple markers at this line
- Unknown property
- Syntax error, property value is missing
- Expected token `:'
- mismatched input '(' expecting RBRACE

 

 zain.authorizationFailure.connect(openDialog);
    function openDialog()
    {
        myDialog.open();
    }

 

defined in my hpp

 

    signals:
        void authorizationFailure();

 

in c++ i cal emit authorizationFailure();


Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: send a signal via c++ to qml

I did this

 

import bb.cascades 1.0
import com.zain.tools 1.0



NavigationPane {

  
    function openDialog()
    {
    myDialog.open();
    }
    id: nav
    objectName: "myNavi"
    onCreationCompleted: {
        Application.menuEnabled = false;
        //timer.start()
      zain.authorizationFailure().connect(openDialog);
    }

    Splash {
        id: splash
    }
    attachedObjects: [
        Dialog {
            id: myDialog
            
            Container {
                horizontalAlignment: HorizontalAlignment.Fill
                verticalAlignment: VerticalAlignment.Fill
                
                Button {
                    horizontalAlignment: HorizontalAlignment.Center
                    text: "Hide Dialog"
                    onClicked: myDialog.close()
                }
            }
        },
        MyTimer {
            id: timer
            interval: 5000
            onTimeout: {

                nav.remove(splash)
                //nav.push(main.createObject())
                stop()
            }
        },
        ComponentDefinition {
            id: main
            source: "ServerSelection.qml"
        },
        ImagePaintDefinition {
            id: back
            repeatPattern: RepeatPattern.XY
            image: tracker.image
        },
        ImageTracker {
            id: tracker
        }
    ]

}

 

 

/*
 * Copyright (c) 2011-2013 BlackBerry Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ApplicationUI_HPP_
#define ApplicationUI_HPP_

#include <QObject>
#include <QString>
#include <bb/cascades/NavigationPane>
namespace bb
{
namespace cascades
{
class Application;
class LocaleHandler;
}
}

class QTranslator;

/*!
 * @brief Application object
 *
 *
 */

class ApplicationUI : public QObject
{
	Q_OBJECT
public:
	ApplicationUI(bb::cascades::Application *app);
	virtual ~ApplicationUI() { }
	static bb::cascades::NavigationPane* myNavi;
	public slots:
	void onReplyAuthorizationFinished();
	Q_INVOKABLE
		void readAuthorization(QVariant);
	private slots:
	void onSystemLanguageChanged();
	signals:
	    void authorizationFailure();
	private:
	QTranslator* m_pTranslator;
	bb::cascades::LocaleHandler* m_pLocaleHandler;
};

#endif /* ApplicationUI_HPP_ */

 

 

on emit authorizationFailure() ..its not working. i have set my qml->setContextProperty("zain", this);

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: send a signal via c++ to qml

i also get ReferenceError: Can't find variable: zain at times

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: send a signal via c++ to qml

my c++ code

 

/*
 * Copyright (c) 2011-2013 BlackBerry Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "applicationui.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include "timer.hpp"
#include <bb/cascades/Dialog>
#include <bb/cascades/Application>
#include <bb/platform/HomeScreen>
#include <bb/platform/DeviceLockState>
#include <bb/cascades/DropDown>
#include <bb/cascades/Label>
#include <bb/cascades/CheckBox>
#include <bb/cascades/ActivityIndicator>
#include <bb/cascades/TextField>
#include <bb/cascades/Container>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include <bb/cascades/StackLayout>
#include <bb/cascades/StackLayoutProperties>
#include <bb/cascades/NavigationPane>
#include <bb/cascades/NavigationPaneProperties>
#include <bb/cascades/Page>
#include <bb/system/phone/Phone>
#include <bb/cascades/ListView>
#include <bb/cascades/DataModel>
#include <bb/data/JsonDataAccess>
#include <bb/cascades/QListDataModel>
#include <QUuid>
#include <bb/system/SystemDialog>
#include <bb/PPsObject>
#include <bb/system/InvokeRequest>
#include <bb/system/InvokeManager>
#include <bb/system/InvokeTargetReply>
#include <bb/device/DisplayInfo>
#include <bb/platform/Notification>
#include <bb/platform/NotificationDialog>
#include <bb/platform/NotificationError>
#include <bb/platform/NotificationResult>

using namespace bb::platform;
using namespace bb::system;
using namespace bb::cascades;
using namespace bb::data;
using namespace bb::device;
AbstractPane *root;
static NavigationPane* myNavi;
NavigationPane* ApplicationUI::myNavi;
int selIndex = -1;
QString deviceUUID = "";
QString auth_token = "";
QTimer *timer = new QTimer();
ApplicationUI::ApplicationUI(bb::cascades::Application *app) :
        						QObject(app)
{

	m_pTranslator = new QTranslator(this);
	m_pLocaleHandler = new LocaleHandler(this);
	bool res = QObject::connect(m_pLocaleHandler,
			SIGNAL(systemLanguageChanged()), this,
			SLOT(onSystemLanguageChanged()));
	connect(timer, SIGNAL(timeout()), this, SLOT(reset()));
	timer->setSingleShot(true);
	Q_ASSERT(res);
	// Since the variable is not used in the app, this is added to avoid a
	// compiler warning
	Q_UNUSED(res);
	// initial load
	onSystemLanguageChanged();

	//timer for splashscreen
	qmlRegisterType<Timer>("com.zain.tools", 1, 0, "MyTimer");
	QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
	DisplayInfo display;
	int width = display.pixelSize().width();
	int height = display.pixelSize().height();
	QDeclarativePropertyMap* displayProperties = new QDeclarativePropertyMap;
	displayProperties->insert("width", QVariant(width));
	displayProperties->insert("height", QVariant(height));
	qml->setContextProperty("DisplayInfo", displayProperties);
	root = qml->createRootObject<AbstractPane>();


	QSettings settings("Netvariant", "Zain");
	deviceUUID = settings.value("uuid").toString();
	if(deviceUUID.isNull() || deviceUUID == "")
	{
		deviceUUID = QUuid::createUuid().toString();
		settings.setValue("uuid", deviceUUID);
	}
	else
	{

	}


	if (!qml->hasErrors()) {
	//	myNavi = dynamic_cast<NavigationPane*>(root);

		qml->setContextProperty("zain", this);
	}

	try
		{
			QNetworkAccessManager* netManager = new QNetworkAccessManager(this);

			QString locale_string = QLocale().name();
			if(locale_string == "ar")
				locale_string == "ar";
			else
				locale_string = "en";
			const QUrl url("some url");
			QNetworkRequest request(url);
			request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
					QNetworkRequest::AlwaysNetwork);
			QNetworkReply* reply = netManager->get(request);

			bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onReplyAuthorizationFinished()));
			Q_ASSERT(ok);
			Q_UNUSED(ok);
		}
		catch(...)
		{

		}

	Application::instance()->setScene(root);
}
void ApplicationUI::onReplyAuthorizationFinished()
{

	try
	{
		QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());


		QString response;
		if (reply) {
			if (reply->error() == QNetworkReply::NoError) {
				const int available = reply->bytesAvailable();
				if (available > 0) {
					const QByteArray data(reply->readAll());
					//	QVariant jsonva(QString(data)); //= Json::parse(QString(data), true);
					// JsonDataAccess json = JsonDataAccess::loadFromBuffer(QString(data));
					JsonDataAccess jda;
					QVariant jsonva = jda.loadFromBuffer(QString(data));
					//QVariantList list = jsonva.toList;
					QVariant list = jsonva;
					emit readAuthorization(list);

				}
			}
			reply->deleteLater();

		}
	}
	catch(...)
	{

	}
}
void ApplicationUI::readAuthorization(QVariant list) {
	try
	{
		//QVariantMap map;
		QVariantMap childJsonv1a = list.toMap();
		QVariantMap childJsonva2 = childJsonv1a["DATA"].toMap();
		QVariant childJsonva = childJsonva2["AUTH_TOKEN"];
		QVariant status = childJsonva2["STATUS"];
		auth_token = childJsonva.toString();
		qDebug() << auth_token;
		//if(status.isNull() || status.toString().isNull() || status.toString() == "")
		{
			emit authorizationFailure();

		}
	}
	catch(...)
	{

	}

}
void ApplicationUI::onSystemLanguageChanged()
{
	QCoreApplication::instance()->removeTranslator(m_pTranslator);
	// Initiate, load and install the application translation files.
	QString locale_string = QLocale().name();
	QString file_name = QString("Zain_%1").arg(locale_string);
	if (m_pTranslator->load(file_name, "app/native/qm")) {
		QCoreApplication::instance()->installTranslator(m_pTranslator);
	}
	qDebug() << auth_token;
}

 

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: send a signal via c++ to qml

i think i got the error but dont know how to fix. its calling qmlsetcontextproperty after oncreationobjectocmpleted.

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: send a signal via c++ to qml

fixed it this ay

 

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

	if (!qml->hasErrors()) {
		qml->setContextProperty("zain", this);
	}
	DisplayInfo display;
	int width = display.pixelSize().width();
	int height = display.pixelSize().height();
	QDeclarativePropertyMap* displayProperties = new QDeclarativePropertyMap;
	displayProperties->insert("width", QVariant(width));
	displayProperties->insert("height", QVariant(height));
	qml->setContextProperty("DisplayInfo", displayProperties);
	root = qml->createRootObject<AbstractPane>();

 

but now i get

 

file:///apps/com.NETVARIANT.Zain.testDev_ARIANT_Zain24730e25/native/assets//main.qml:18: TypeError: Result of expression 'zain.authorizationFailure()' [undefined] is not an object

Highlighted
New Developer
Posts: 10
Registered: ‎03-11-2014
My Device: Z10
My Carrier: BITE Latvia

Re: send a signal via c++ to qml

Maybe a little late, but: In your QML try to replace
zain.authorizationFailure().connect(openDialog)
with this
zain.authorizationFailure.connect(openDialog)
That should do it, assuming you have correctly setContextProperty in Application constructor, and the signal is emitted.