Namespaces
Variants
Actions

Turning on and off the screen of MeeGo Harmattan using QML and C++

Jump to: navigation, search

This article explains how to turn on and off the screen of a MeeGo Harmattan device programatically from QML using C++ and QML binding.

Article Metadata

Tested with
SDK: Nokia Qt SDK 1.1.3
Devices(s): Nokia N950

Compatibility
Platform(s): MeeGo Harmattan
Device(s): All

Article
Keywords: QmDisplayState,qmdisplaystate.h,qmsystem
Created: sabb0ur ()
Last edited: hamishwillee (11 Oct 2012)

Contents

Introduction

This code snippet will enable you to programatically query the display state of the device, whether it is On or Off (or Dimmed), as well as turn On/Off the display from button clicks on the QML interface.

It uses QML/C++ binding and the QmDisplayState class to do so.

Project file

CONFIG+= qmsystem2

mycustomclass.h

#ifndef MYCUSTOMCLASS_H 
#define MYCUSTOMCLASS_H
 
#include <qmdisplaystate.h>
#include <QDebug>
#include <QObject>
 
class MyCustomclass : public QObject
{
Q_OBJECT
public:
MyCustomclass();
 
MeeGo::QmDisplayState displayState;
 
Q_INVOKABLE void printDisplayState();
Q_INVOKABLE void turnOffDisplay();
Q_INVOKABLE void turnOnDisplay();
 
};
 
#endif // MYCUSTOMCLASS_H

myscustomclass.cpp

#include "mycustomclass.h"
 
MyCustomclass::MyCustomclass() {
}
 
 
void MyCustomclass::printDisplayState() {
if (displayState.get() == MeeGo::QmDisplayState::On) {
qDebug() << "The display is on!";
}
else if(displayState.get() == MeeGo::QmDisplayState::Off) {
qDebug() << "The display is off!";
}
}
 
void MyCustomclass::turnOffDisplay() {
qDebug() << "Turning off display";
displayState.set(MeeGo::QmDisplayState::Off);
}
 
void MyCustomclass::turnOnDisplay() {
qDebug() << "Turning on display";
displayState.set(MeeGo::QmDisplayState::On);
}

main.cpp

#include <QDebug>
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QDeclarativeContext>
#include "mycustomclass.h"
 
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
 
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
 
MyCustomclass myObject;
viewer.setMainQmlFile(QLatin1String("qml/WakeUpAndSleep/main.qml"));
viewer.rootContext()->setContextProperty("myObject", &myObject);
 
viewer.showExpanded();
 
return app.exec();
}

main.qml

import QtQuick 1.1 
import com.nokia.meego 1.0
 
PageStackWindow {
id: appWindow
 
initialPage: mainPage
 
MainPage {
id: mainPage
}
}

MainPage.qml

import QtQuick 1.1 
import com.nokia.meego 1.0
 
Page {
 
Timer {
id: turnOnScreenTimer
interval: 5000; running: false; repeat: false
onTriggered: {
console.log("Timer running to turn on screen");
myObject.turnOnDisplay();
myObject.printDisplayState();
}
}
 
Label {
id: label
anchors.centerIn: parent
text: qsTr("Sleep and wakeup demo")
visible: false
}
 
Button{
id: displayStateButton
anchors {
horizontalCenter: parent.horizontalCenter
top: label.bottom
topMargin: 10
}
text: qsTr("Display state")
 
onClicked: {
myObject.printDisplayState();
}
}
 
Button{
id: turnOffScreenButton
anchors {
horizontalCenter: parent.horizontalCenter
top: displayStateButton.bottom
topMargin: 10
}
text: qsTr("Turn off screen")
onClicked: {
myObject.printDisplayState();
myObject.turnOffDisplay();
myObject.printDisplayState();
}
}
 
Button{
id: turnOffForFiveButton
anchors {
horizontalCenter: parent.horizontalCenter
top: turnOffScreenButton.bottom
topMargin: 10
}
text: qsTr("Turn off screen for 5 seconds")
onClicked: {
myObject.printDisplayState();
myObject.turnOffDisplay();
myObject.printDisplayState();
turnOnScreenTimer.start();
}
 
}
}


Test application

Download the zipped project here File:WakeUpAndSleep.zip which includes a .deb installation.

This page was last modified on 11 October 2012, at 04:20.
138 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved