×
Namespaces

Variants
Actions

Searching device platform in QML

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia E7, Nokia N8, Nokia N900
Compatibility
Platform(s): Symbian
Maemo
Symbian
Article
Keywords: platform, Q_INVOKABLE, QDeclarativeView
Created: tepaa (12 Jan 2011)
Last edited: kiran10182 (31 Oct 2013)


Contents

Overview

This snippet shows how to search used platform in QML code using Qt. QML does not support that kind of feature, but we can use Qt side for that.

This snippet was used in the Nokia Developer example QuickHit


Qt side

Q_INVOKABLE methods of this MyObject Qt class can be called from QML side after setting class as root context property of QDeclarativeView, see more QDeclarativeView::rootContext()

MyObject* myobject = new MyObject(this);
rootContext()->setContextProperty("MyObject", myobject);

Qt class that have to stored into QDeclarativeView as root context property

#include <QObject>
 
class MyObject : public QObject
{
Q_OBJECT
public:
MyObject(QObject* parent);
~MyObject();
 
// Q_INVOKABLE enables method to be invoked via the meta-object system
// and that for from QML
Q_INVOKABLE QVariant isSymbian();
Q_INVOKABLE QVariant isMaemo();
Q_INVOKABLE QVariant isWindows();
};


MyObject::MyObject(QObject* parent) :QObject(parent)
{
}
 
MyObject::~MyObject()
{
}
QVariant MyObject::isSymbian()
{
#ifdef Q_OS_SYMBIAN
return QVariant(true);
#else
return QVariant(false);
#endif
}
 
QVariant MyObject::isMaemo()
{
#ifdef Q_WS_MAEMO_5
return QVariant(true);
#else
return QVariant(false);
#endif
}
 
QVariant MyObject::isWindows()
{
#ifdef Q_OS_WIN
return QVariant(true);
#else
return QVariant(false);
#endif
}



QML side

Example of QML Item from which some QMLFunction() function is Qt side called and used platform asked

Item {
id: someId
 
function someQMLFunction() {
if (MyObject.isSymbian()) {
console.log("Symbian platform")
}
}
}


Postconditions

QML side knows used platform.


See also

Q_INVOKABLE

QVariant

QDeclarativeView::rootContext()

This page was last modified on 31 October 2013, at 19:33.
38 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×