如果在Ubuntu QML应用中在应用一启动时就得到屏幕的分辨率

对于有些应用来说,获取屏幕分辨率这个信息可能是重要的。比如有些游戏或阅读器应用,希望在应用启动后,马上得到屏幕的分辨率,这样可以和容易地适配不同屏幕尺寸的手机或装置。有些应用可以是用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_OBJECT

public:
    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.0
import Ubuntu.Components 1.1
import Screen 1.0
import 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: 768
qml: screen height: 1280
qml: SCREEN width: 0
qml: SCREEN height: 0

我们可以看出来,MyScreen得到了正确的分辨率,但是“Screen.width”及“Screen.height”得到的是“0”。

整个项目的源码在:git clone https://gitcafe.com/ubuntu/screen.git

时间: 2024-08-30 14:09:24

如果在Ubuntu QML应用中在应用一启动时就得到屏幕的分辨率的相关文章

如何在Ubuntu QML应用中播放视频

在这篇文章中,我们将介绍如何在Ubuntu QML应用中播放一个视频.为了实现方便,我们可以事先用手机录下一个视频,并置于我们已经创建好的项目中. 首先,我们利用Ubuntu SDK来创建一个"QML app with Simple UI (qmake)"的项目.我们修改我们的Main.qml文件如下: import QtQuick 2.0 import Ubuntu.Components 1.1 import QtMultimedia 5.0 /*! \brief MainView

如何在Ubuntu QML应用中播放音乐

昨天我看见一个开发者发了一个问题,询问如何播放声音.目前由于一些原因在模拟器中没法测试.其实,播放声音很容易.如果使用qmake的话,可能需要做一些修改才可以正确地在手机上播放. 我们首先使用SDK来创建一个简单的项目(QML app with Simple UI "qmake").我们记得修改我们的Main.qml如下: import QtQuick 2.0 import Ubuntu.Components 1.1 import QtMultimedia 5.0 /*! \brief

怎么在Ubuntu QML应用中侦测到Swipe手势

我们知道在触屏的手机中,可以利用手势可以产生一下动作.特别是Ubuntu手机,手势的操作利用的非常多.那么怎么可以在QML应用中侦测到手势呢?我以前在我的Flickr应用使用到一个手势的侦测.今天我们利用网上的一个例程来,做一个例子.这个例程更加具有可以重复利用性.我们的参阅代码地址:https://gist.github.com/kovrov/1742405 SwipeArea.qml /* This code was written by Sergejs Kovrovs and has be

如何在Ubuntu QML应用中震动(vibration)

对于有些QML应用来说,震动是非常重要的一个功能.特别是对一下游戏来说.那么我们怎么在QML应用中震动呢? 我们官方有一个API HapticsEffect,这个API的功能就是让我们的应用来震动的.使用这个API非常容易: import QtQuick 2.0 import Ubuntu.Components 1.1 import QtFeedback 5.0 /*! \brief MainView with a Label and Button elements. */ MainView {

如何在Ubuntu QML应用中进行语言录音

在QML API中,目前并没有一个相应的API来进行录音.我们必须使用Qt C++ API QAudioRecorder来进行录音的工作.在这篇文章中,我们来介绍如何使用这个API来进行录音. 首先,我们来创建一个"QML App with C++ plugin (qmake)"模版的应用.注意qmake的项目必须是在15.04及以上的target上才可以运行. 为了录音,我创建了一个叫做"AudioRecorder"的类: audiorecorder.h #ifn

springBoot和MyBatis整合中出现SpringBoot无法启动时处理方式

在springBoot和Myatis   整合中出现springBoot无法启动   并且报以下错误 Description: Field userMapper in cn.lijun.controller.UserController required a bean of type 'cn.lijun.mapper.UserMapper' that could not be found. Action: Consider defining a bean of type 'cn.lijun.ma

如何在Ubuntu QML应用中使用Push Notification

我们知道目前Ubuntu手机平台有些类似iPhone平台,是一个单任务的操作系统,虽然系统本身具有多任务的功能.如果当前的应用被推到后台的话,应用将会被自动挂起,而不会被系统所运行.在这个时候如果我们的应用需要等待一个消息,比如就想微信之类的信息,我们就要使用Ubuntu平台所提供的Push Notification机制来实现我们的类似多任务的东西.当通知被收到后,我们就可以直接点击接受到的通知,应用又会被重新运行到前台. 关于Push notification,在我们的开发者网站上,有一篇文章

如何在Ubuntu QML应用中判断应用的方位(landscape或portrait)

我们知道对于一些应用来说,判断方位可以使得我们可以重新定位我们的应用的布局,以使得我们的应用在不同的方位中更加合理及好看.在这篇文章中,我们来介绍如何来侦测应用方位的变化. 我们首先来创建一个我们自己的简单的QML应用.对于大多数的QML应用来说,一般是含有一个"MainView"的: MainView { id: root // objectName for functional testing purposes (autopilot-qt5) objectName: "m

如何在Ubuntu QML应用中实现一个垂直的Slider

我们在使用Ubuntu SDK中的Slider的时候,我们发现,它没有orientation的属性尽管在Qt官方网站的slider是有这个属性的.在默认的情况下,这个Slider是水平的.那么我们该如实现这个呢? 我们的任何一个QML Item都有一个属性叫做rotation.我们可以通过这个属性来得到一个旋转90度的水平Slider.这样我们就可以用如下的代码来实现了: import QtQuick 2.0 import Ubuntu.Components 1.1 /*! \brief Mai