How to determine the application's orientation (Landscape or portrait) in Ubuntu QML applications

Source: Internet
Author: User
Tags git clone

We know that for some applications, judging the orientation allows us to reposition the layout of our application so that our applications are more reasonable and good-looking in different directions. In this article, we'll show you how to detect changes in application orientation.


Let's start by creating a simple QML application of our own. For most QML applications, a "MainView" is generally included:


MainView {    id:root    //ObjectName for functional testing purposes (AUTOPILOT-QT5)    objectName: "MainView"    //note! ApplicationName needs to match the "name" field of the the click Manifest    ApplicationName: "orientation.liu- Xiao-guo "/* This property 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) Property    bool Islandscape: Pagestack.width > Pagestack.height? True:false    onwidthchanged: {        console.log ("Main width is changed:" + width)    } ...}


To enable our application to move in both horizontal and vertical directions, we must open this switch:


    Automaticorientation:true

We also try to use:

    Onwidthchanged: {        console.log ("Main width is changed:" + width)    }

No matter how we shake our phone, we find that the method above has been called only once and has not been called. So it's not possible to use this method.


We also try to use the same trick in the page:


    Page {        id:page1        title:i18n.tr ("Orientation")        anchors.fill:parent        onwidthchanged: {            Console.log ("page width is changed:" + width);        }    ...    }

We found that:


Qml:pagestack height is changed:768qml:orientation:1qml:page width are changed:768qml:pagestack width is changed:76 8qml:root width:768qml:pagestack height is changed:1222qml:orientation:4qml:page width is changed:1222

There is a change in the width of this page. For convenience, we used the Pagestack to detect changes in width:


    Pagestack {        id:pagestack        anchors.fill:parent        onwidthchanged: {            console.log ("Pagestack width is Changed: "+ width);            Console.log ("root width:" + root.width);        }        Onheightchanged: {            console.log ("pagestack height is changed:" + height);        }    }

In our Mainview, we can define a variable:


property bool IsLandscape:pageStack.width > Pagestack.height? True:false

With this variable, we have a good idea of where our application is in the same direction.

In addition, we can also detect changes in the position of the mobile phone by orientationsensor:

    function Displayorientation (reading) {Orientation.text = "Unknown" Console.log ("Orientation:" + readin        G.orientation);        if (reading.orientation = = = Orientationreading.topup) {orientation.text = "topup";        } else if (reading.orientation = = = Orientationreading.topdown) {orientation.text = "topdown";        } else if (reading.orientation = = = Orientationreading.leftup) {orientation.text = "leftup";        } else if (reading.orientation = = = Orientationreading.rightup) {orientation.text= "Rightup";        } else if (reading.orientation = = = Orientationreading.facedown) {orientation.text = "facedown";        } else if (reading.orientation = = = Orientationreading.faceup) {orientation.text = "faceup";  }} orientationsensor {id:sensor active:true alwayson:true onreadingchanged:        {displayorientation (reading); }    }


To run our application, you can see:




The source code for the entire project is: Git clone https://gitcafe.com/ubuntu/oirentation.git



How to determine the application's orientation (Landscape or portrait) in Ubuntu QML applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.