Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Qt Tutorials for Intermediate : How to vibrate the device using S60 API on Qt

From Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s):
Symbian
Article
Created: neois (22 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

This article shows how to provide haptic feedback (vibration) in a Qt application by calling the underlying Symbian C++ API. Note that a real application would use the Qt Mobility 1.1 Feedback API.

Contents

Prerequisites

This article integrates Symbian API with Nokia Qt SDK and requires previous knowledge to fully understand this article. The reader is assumed to have this knowledge.

How it works

Nokia Qt SDK includes a S60 API library named HW Resource Manager Vibra API (CHWRMVibra). This is used for controlling vibration on device.

Code Snippet

hapticfeedback.h

/*************************
* hapticfeedback.h
*************************/

 
#ifndef HAPTICFEEDBACK_H
#define HAPTICFEEDBACK_H
 
#include <QObject>
 
#ifdef Q_OS_SYMBIAN
#include <hwrmvibra.h>
#endif
 
class HapticFeedback
{
public:
HapticFeedback();
 
public:
void initHapticControl();
void destroyHapticControl();
void vibrate();
 
#ifdef Q_OS_SYMBIAN
CHWRMVibra* iVibrate;
#endif
 
};
 
#endif // HAPTICFEEDBACK_H

hapticfeedback.cpp

/*************************
* hapticfeedback.cpp
*************************/

 
#include "hapticfeedback.h"
 
HapticFeedback::HapticFeedback()
#ifdef Q_OS_SYMBIAN
: iVibrate(NULL)
#endif
{
}
 
void HapticFeedback::initHapticControl()
{
#ifdef Q_OS_SYMBIAN
if (iVibrate)
return;
iVibrate = CHWRMVibra::NewL();
#endif
}
 
void HapticFeedback::destroyHapticControl()
{
#ifdef Q_OS_SYMBIAN
if (iVibrate)
{
delete iVibrate;
iVibrate = NULL;
}
#endif
}
 
void HapticFeedback::vibrate()
{
#ifdef Q_OS_SYMBIAN
if (iVibrate)
{
TRAPD(err, iVibrate->StartVibraL(2000, 50));
}
#else
#endif
}

Add link library -lhwrmvibraclient to LIBS inside symbian scope in .pro file

symbian {
    ....
    LIBS += -lcone -leikcore -lavkon -lhwrmvibraclient
    ....
}

How to use

Initialize HapticFeedback object.

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
....
hapticFeedbackControl = new HapticFeedback;
hapticFeedbackControl->initHapticControl();
....
}

Don't forget to destroy it in destructor

MainWindow::~MainWindow()
{
if (hapticFeedbackControl)
{
hapticFeedbackControl->destroyHapticControl();
delete hapticFeedbackControl;
hapticFeedbackControl = NULL;
}

To vibrate the device simply call this command:

        hapticFeedbackControl->vibrate();

Happy vibration ;)

Author

--neois

This page was last modified on 11 October 2012, at 01:18.
80 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.

×