For some QML applications, vibration is a very important function. Especially for the game. So how do we vibrate in the QML application?
We officially have an API Hapticseffect, the function of this API is to let our application to vibrate. Using this API is easy:
Import QtQuick 2.0import ubuntu.components 1.1import qtfeedback 5.0/*! \brief MainView with a Label and Button Elements.*/mainview {//ObjectName for functional testing purposes (autopilot- QT5) ObjectName: "MainView"//note! ApplicationName needs to match the "name" field of the click Manifest ApplicationName: "Vibration.liu-xiao-guo"/* The enables the application to change orientation when the device is rotated. The default is False. *///automaticorientation:true//Removes the old toolbar and enables new features of the new header. Usedeprecatedtoolbar:false width:units.gu (height:units.gu) page {title:i18n.tr ("vibration") Hapticseffect {id:rumbleeffect attackintensity:0.0 attacktime:250 in tensity:1.0 duration:100 fadetime:250 fadeintensity:0.0} Column { Spacing:units.gu (1) anchors {margins:units.gu (2) Fill:parent} Button {OB Jectname: "button" Width:parent.width text:i18n.tr ("Vibrate me!") OnClicked: {Rumbleeffect.start (); Plays a rumble Effect}}}}
Here we import the libraries we need:
Import Qtfeedback 5.0
Then, instantiate our Hapticseffect:
Hapticseffect { id:rumbleeffect attackintensity:0.0 attacktime:250 intensity:1.0 Duration: fadetime:250 fadeintensity:0.0 }
When we press our button, we start shaking. Of course this must be on the phone to be able to test.
The source code for the entire project is: Git clone https://gitcafe.com/ubuntu/vibration.git
How to vibrate in Ubuntu QML applications (Vibration)