【QT】在Qt中获取屏幕大小

如何在Qt中获取屏幕大小呢?


首先要调用QApplication类中的一个static方法,如下:

QDesktopWidget *QApplication::desktop()

QApplication类用来管理GUI应用程序的控制流和主要设置,其派生关系如下:

QApplicationQGuiApplicationQCoreApplicationQObject

在主程序中,一定是QApplication而非QGuiApplicationQCoreApplication,否则会有

错误QWidget: Cannot create a QWidget without QApplication

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    // ...
    return app.exec();
}


QDesktopWidget类提供了访问多屏幕信息的接口,有时候我们的系统由多个屏幕组成,这时我们可以把这些屏幕当作多个桌面来对待,也可以看成是一个大的虚拟桌面。

>>QDesktopWidget::screenGeometry()用来获得屏幕大小,有三个重载函数:

const QRect QDesktopWidget::screenGeometry(int screen = -1) const

const QRect QDesktopWidget::screenGeometry(const QWidget *widget) const

const QRect QDesktopWidget::screenGeometry(const QPoint &p) const

>>QDesktopWidget::availableGeometry()用来获得屏幕的有效区域,如在screenGeometry()的基础上去掉桌面菜单栏等占据的区域,有三个重载函数:

const QRect QDesktopWidget::availableGeometry(int screen = -1) const

const QRect QDesktopWidget::availableGeometry(const QWidget *widget) const

const QRect QDesktopWidget::availableGeometry(const QPoint &p) const

>>QDesktopWidget继承自QWidget,所以通过QWidget中的width()height()也可以获得屏幕尺寸,但是有多个屏幕时,这两个接口就用非所用了,因为返回的是整个大的虚拟桌面的大小。


下面是一个完整的例子,展示了获取屏幕尺寸的方法,并将其设置为QML上下问属性,这样在QML文件中也就可以访问了。

#include <QApplication>
#include <QQuickView>
#include <QtQml>
#include <QDesktopWidget>
#include <QRect>
#include <iostream>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    int virtualWidth = 0;
    int virtualHeight = 0;
    int availableWidth = 0;
    int availableHeight = 0;
    int screenWidth = 0;
    int screenHeight = 0;

    QDesktopWidget *deskWgt = QApplication::desktop();
    if (deskWgt) {
        virtualWidth = deskWgt->width();
        virtualHeight = deskWgt->height();
        std::cout << "virtual width:" << virtualWidth << ",height:" << virtualHeight << std::endl;

        QRect availableRect = deskWgt->availableGeometry();
        availableWidth = availableRect.width();
        availableHeight = availableRect.height();
        std::cout << "available width:" <<availableWidth << ",height:" << availableHeight << std::endl;

        QRect screenRect = deskWgt->screenGeometry();
        screenWidth = screenRect.width();
        screenHeight = screenRect.height();
        std::cout << "screen width:" <<screenWidth << ",height:" << screenHeight << std::endl;
    }

    QQuickView view;
    view.setSource(QUrl(QStringLiteral("qrc:///main.qml")));
    if (view.rootContext()) {
        view.rootContext()->setContextProperty("virtualWidth", virtualWidth);
        view.rootContext()->setContextProperty("virtualHeight", virtualHeight);
        view.rootContext()->setContextProperty("availableWidth", availableWidth);
        view.rootContext()->setContextProperty("availableHeight", availableHeight);
        view.rootContext()->setContextProperty("screenWidth", screenWidth);
        view.rootContext()->setContextProperty("screenHeight", screenHeight);
    }
    view.show();
    return app.exec();
}

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值