如果在Ubuntu QML應用中在應用一啟動時就得到螢幕的解析度,ubuntuqml

來源:互聯網
上載者:User

如果在Ubuntu QML應用中在應用一啟動時就得到螢幕的解析度,ubuntuqml

對於有些應用來說,擷取螢幕解析度這個資訊可能是重要的。比如有些遊戲或閱讀器應用,希望在應用啟動後,馬上得到螢幕的解析度,這樣可以和容易地適配不同螢幕尺寸的手機或裝置。有些應用可以是用QtQuick.Window的Screen來得到這個資訊,但是我們可以看一下在文章中如下的提醒:


Note that the Screen type is not valid at Component.onCompleted, because the Item or Window has not been displayed on a screen by this time.


這也就是說我們不能使用上述的方法來得到螢幕的尺寸。通過我的實驗。顯示的結果為“0”。


為了能夠在應用啟動時得到螢幕的解析度,我們可以使用C++的代碼來實現。


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 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::primaryScreen();QSize screenSize =  screen->size();qDebug() << "width: " << screenSize.width();m_width = screenSize.width();m_height = screenSize.height();}

我們可以使用Ubuntu SDK提供的“QML App with C++ plugin (cmake)”來建立一個應用來測試。測試應用的代碼如下:


import QtQuick 2.0import Ubuntu.Components 1.1import Screen 1.0import QtQuick.Window 2.0/*!    \brief MainView with Tabs element.           First Tab has a single Label and           second Tab has a single ToolbarAction.*/MainView {    // objectName for functional testing purposes (autopilot-qt5)    objectName: "mainView"    // Note! applicationName needs to match the "name" field of the click manifest    applicationName: "screen.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(50)    height: units.gu(76)    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: parent.width                text: i18n.tr("Tap me!")                onClicked: {                    myType.helloWorld = i18n.tr("..from Cpp Backend")                }            }        }        Component.onCompleted: {            console.log("screen width: " + screen.width);            console.log("screen height: " + screen.height);            console.log("SCREEN width: " + Screen.width );            console.log("SCREEN height: " + Screen.height)        }    }}


我們應用的輸出為:


qml: screen width: 768qml: screen height: 1280qml: SCREEN width: 0qml: SCREEN height: 0

我們可以看出來,MyScreen得到了正確的解析度,但是“Screen.width”及“Screen.height”得到的是“0”。


整個項目的源碼在:git clone https://gitcafe.com/ubuntu/screen.git


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.