QT开发中文网

"QDesktopServices"获取桌面长宽

您可以使用QDesktopWidget类来获取桌面的长宽信息。以下是一个示例代码片段,演示如何使用QDesktopWidget获取主屏幕的宽度和高度:

#include <QApplication>
#include <QDesktopWidget>

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

    QDesktopWidget *desktop = QApplication::desktop();
    int screenWidth = desktop->screenGeometry().width();
    int screenHeight = desktop->screenGeometry().height();

    qDebug() << "Screen Width: " << screenWidth;
    qDebug() << "Screen Height: " << screenHeight;

    return app.exec();
}

通过调用screenGeometry()函数可以获得主屏幕的几何信息,包括位置、大小等。然后,可以使用width()height()函数分别获取宽度和高度。

请注意,在Qt 5之后,推荐使用QGuiApplication::primaryScreen()QScreen::geometry()来获取屏幕信息。这种方法提供了更多灵活性,并且适用于多显示器环境下。

#include <QGuiApplication>
#include <QScreen>

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

    QScreen *primaryScreen = QGuiApplication::primaryScreen();
    QRect screenGeometry = primaryScreen->geometry();

    int screenWidth = screenGeometry.width();
    int screenHeight = screenGeometry.height();

    qDebug() << "Screen Width: " << screenWidth;
    qDebug() << "Screen Height: " << screenHeight;

    return app.exec();
}

以上两种方法都可以用来获取桌面的长宽信息。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=9833

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?