×
Namespaces

Variants
Actions
(Difference between revisions)

Showing QML elements based on platform

From Nokia Developer Wiki
Jump to: navigation, search
somnathbanik (Talk | contribs)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Maemo]][[Category:MeeGo]][[Category:Qt]][[Category:Qt Quick]]
+
{{ArticleMetaData <!-- v1.2 -->
 +
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 +
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20110506
 +
|author= [[User:Suri.nandita]]
 +
}}
 +
[[Category:Maemo]][[Category:MeeGo Harmattan]][[Category:Qt]][[Category:Qt Quick]][[Category:Symbian]][[Category:Porting]]
 +
{{FeaturedArticle|timestamp=20110515}}
 +
 
 
==Overview==
 
==Overview==
{{Abstract|This article mainly shows how to determine the platform on which our program is running and decide in run time what elements to be shown in QML file.}}
+
{{Abstract|This article shows how to determine the platform on which an application is running and decide at run time what elements are to be shown in the QML file.}}
  
 
Example addresses three platforms :  '''Symbian''' , '''Maemo5''' and  '''Maemo6'''
 
Example addresses three platforms :  '''Symbian''' , '''Maemo5''' and  '''Maemo6'''
  
'''OSDetails''' is a {{Icode|QObject}}  class having invokable function which determines the platform.  For calling a C++ function from QML , the function must be either a Qt Slot or it should be marked with
+
'''OSDetails''' is a {{Icode|QObject}}  class having invokable function which determines the platform.  For calling a C++ function from QML, the function must be either a Qt Slot or it should be marked with
 
{{Icode|Q_INVOKABLE}} macro. With this macro, we register the function with '''meta-object''' system enabling it to be invoked using {{Icode|QMetaObject::invokeMethod()}}
 
{{Icode|Q_INVOKABLE}} macro. With this macro, we register the function with '''meta-object''' system enabling it to be invoked using {{Icode|QMetaObject::invokeMethod()}}
  
Line 11: Line 35:
 
    
 
    
 
   
 
   
<code cpp>
+
<code cpp-qt>
 
#ifndef OSDETAILS_H
 
#ifndef OSDETAILS_H
 
#define OSDETAILS_H
 
#define OSDETAILS_H
Line 48: Line 72:
 
==OSDetails Class Implementation==  
 
==OSDetails Class Implementation==  
  
<code cpp>
+
<code cpp-qt>
  
 
#include "osdetails.h"
 
#include "osdetails.h"
Line 82: Line 106:
 
</code>
 
</code>
  
Set the context property that can hold {{Icode|QObject*}} and {{Icode|QVariant}} values. This C++ object can be modified and read directly in qml. Here, we are setting the '''OSDetails''' object as the context property which can be read in our qml file. {{Icode|setContextProperty()}} sets the name and value of the property. The object can be accessed through the name ("OSdetails") of the property given in this function.
+
Set the context property that can hold {{Icode|QObject*}} and {{Icode|QVariant}} values. This C++ object can be modified and read directly in qml. Here, we are setting the '''OSDetails''' object as the context property which can be read in our qml file. {{Icode|setContextProperty()}} sets the name and value of the property. The object can be accessed through the name ("OSdetails") of the property given in this function.
 
Make sure that the context property is set before the qml file is loaded.
 
Make sure that the context property is set before the qml file is loaded.
  
<code cpp>
+
<code cpp-qt>
  
 
#include <QtGui/QApplication>
 
#include <QtGui/QApplication>
Line 114: Line 138:
 
</code>
 
</code>
  
Based on the platform which can be determined using  {{Icode|getOSName()}} function of '''OSDetails''' class, the elements can be appropriately shown. Text and images are appropriately shown.
+
Based on the platform which can be determined using  {{Icode|getOSName()}} function of '''OSDetails''' class, the elements can be appropriately shown. Text and images are appropriately shown.
  
 
==main.qml==  
 
==main.qml==  
Line 145: Line 169:
 
             name.text = "maemo5"
 
             name.text = "maemo5"
 
         }
 
         }
         else if(OSdetails.getOSName() == 3) {
+
         else if(OSdetails.getOSName() == 3) {
 
             name.text = "Symbian"
 
             name.text = "Symbian"
 
             background.source = "symbian_os_logo.jpg"
 
             background.source = "symbian_os_logo.jpg"

Latest revision as of 04:18, 11 October 2012

Article Metadata
Compatibility
Platform(s):
Symbian
Article
Created: suri.nandita (06 May 2011)
Last edited: hamishwillee (11 Oct 2012)
{{{width}}}
15 May
2011

Contents

[edit] Overview

This article shows how to determine the platform on which an application is running and decide at run time what elements are to be shown in the QML file.

Example addresses three platforms : Symbian , Maemo5 and Maemo6

OSDetails is a QObject class having invokable function which determines the platform. For calling a C++ function from QML, the function must be either a Qt Slot or it should be marked with Q_INVOKABLE macro. With this macro, we register the function with meta-object system enabling it to be invoked using QMetaObject::invokeMethod()

[edit] OSDetails Class Definition

#ifndef OSDETAILS_H
#define OSDETAILS_H
 
#include <QObject>
 
class OSDetails : public QObject
{
Q_OBJECT
 
public:
 
enum OSTYPE {
MAEMO5 = 1,
MAEMO6 = 2,
SYMBIAN = 3,
UNKNOWN = 4
};
 
OSDetails();
 
/*For calling a C++ function from QML,
i. the function must be a qt slot
ii. or a function marked with the Q_INVOKABLE macro */

/* Q_INVOKABLE,registering the function with meta-object system
and enabling it to be invoked using QMetaObject::invokeMethod() */

 
Q_INVOKABLE unsigned int getOSName();
 
};
 
#endif // OSDETAILS_H

[edit] OSDetails Class Implementation

#include "osdetails.h"
#include <QDebug>
 
OSDetails::OSDetails()
{
qDebug() << "\n In Default Constructor";
}
 
/* Function to get the OS detail based on which
qml components can be shown*/

 
unsigned int OSDetails::getOSName()
{
int osName = UNKNOWN;
 
#ifdef Q_WS_MAEMO_6
osName = MAEMO6;
#endif
 
#ifdef Q_OS_SYMBIAN
osName = SYMBIAN;
#endif
 
#ifdef Q_WS_MAEMO_5
osName = MAEMO5;
#endif
 
return osName;
}

Set the context property that can hold QObject* and QVariant values. This C++ object can be modified and read directly in qml. Here, we are setting the OSDetails object as the context property which can be read in our qml file. setContextProperty() sets the name and value of the property. The object can be accessed through the name ("OSdetails") of the property given in this function. Make sure that the context property is set before the qml file is loaded.

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "osdetails.h"
#include <QDeclarativeContext>
 
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
 
QmlApplicationViewer viewer;
OSDetails osdetails;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
 
/*Context property can hold QObject* and QVariant values. This C++ object
can be modified and read directly in qml */

/*Sets the name and value of the property. This name can be used to
call different members and functions of the passed value/object*/

 
viewer.rootContext()->setContextProperty("OSdetails",&osdetails);
viewer.setMainQmlFile(QLatin1String("qml/ostype/main.qml"));
viewer.showExpanded();
 
return app.exec();
}

Based on the platform which can be determined using getOSName() function of OSDetails class, the elements can be appropriately shown. Text and images are appropriately shown.

[edit] main.qml

import QtQuick 1.0
 
Rectangle {
width: 500
height : 500
Image {
id: background
anchors.fill: parent
}
 
Component.onCompleted: {
/* Getting the OS name details from C++ function
and setting appropriate visibility and text values.*/
/* Calling the function using the context property name
set in main.cpp */
 
if(OSdetails.getOSName() == 2){
name.text = "maemo6"
background.source = "maemo_org_logo.png"
 
}
else if(OSdetails.getOSName() == 1) {
background.source = "maemo_org_logo.png"
name.text = "maemo5"
}
else if(OSdetails.getOSName() == 3) {
name.text = "Symbian"
background.source = "symbian_os_logo.jpg"
}
else
name.text = "UnKnown"
}
Rectangle {
id: osname
width :70
height : 30
anchors.right: parent.right
anchors.rightMargin: 30
anchors.top: parent.top
anchors.topMargin: 30
 
Text {
id:name
anchors.centerIn: parent
style: Text.Outline
styleColor: "red"
text:" "
}
}
}
This page was last modified on 11 October 2012, at 04:18.
127 page views in the last 30 days.