For some applications, it may be important to get the screen resolution for this information. For example, some games or reader applications, in the hope that after the application starts, immediately get the screen resolution, so that it can easily adapt to different screen size of the phone or device. Some applications can use Qtquick.window screen to get this information, but we can look at the following reminders in the article:
Note that the screen type was not valid at component.oncompleted, because the Item or Window had not been displayed on a SC Reen by this time.
This means that we cannot use the above method to get the size of the screen. Through my experiment. The result shown is "0".
In order to be able to get the screen resolution when the app starts, we can use C + + code to implement it.
Screen.h
#ifndef screen_h#define screen_h#include <QObject> #include <QGuiApplication> #include <qscreen># Include <qdebug>class screen:public qobject{ q_objectpublic: q_property (int height READ height) q_ property (int width of READ width) explicit screen (Qobject *parent = 0); int height () {return m_height;}; int width () {return m_width;}; Private: int m_height; int m_width;}; #endif//Fileio_h
Screen.cpp
#include "Screen.h" Screen::screen (Qobject *parent): Qobject (parent) {qscreen* screen = qguiapplication::p rimaryscreen (); Qsize screensize = screen->size () qdebug () << "width:" << screensize.width (); m_width = Screensize.width (); m_height = Screensize.height ();}
We can use the Ubuntu SDK to provide "QML app with C + + plugin (cmake)" To create an app to test. The code for the test application is as follows:
Import QtQuick 2.0import ubuntu.components 1.1import screen 1.0import Qtquick.window 2.0/*! \brief MainView with Tabs element. First Tab have a single Label and second Tab have a single Toolbaraction.*/mainview {//ObjectName for Functio NAL testing purposes (AUTOPILOT-QT5) ObjectName: "MainView"//note! ApplicationName needs to match the "name" field of the click Manifest ApplicationName: "Screen.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) myscreen {Id:screen} Page { title:i18n.tr ("App with backend") MyType {Id:mytype component.oncompleted: { Mytype.helloworld = i18n.tr ("Hello world..") } } Column {Spacing:units.gu (1) Anchors {margins:units.gu (2) Fill:parent} label {Id:label objectName: "Label" Text:myType.helloWorld} button {objectName: "button" Width:par Ent.width text:i18n.tr ("Tap me!") onclicked: {Mytype.helloworld = i18n.tr (".. From Cpp backend ')}}} component.oncompleted: {console.log ("Screen W Idth: "+ screen.width); Console.log ("screen height:" + screen.height); Console.log ("screen width:" + screen.width); Console.log ("screen height:" + screen.height)}}}
The output of the application we apply is:
Qml:screen width:768qml:screen Height:1280qml:screen Width:0qml:screen height:0
As we can see, Myscreen gets the correct resolution, but "Screen.width" and "Screen.height" get the "0".
The source code for the entire project is: Git clone https://gitcafe.com/ubuntu/screen.git
If you get the screen resolution in an Ubuntu QML app when the app starts