Qt中如何获取桌面宽度高度

这篇文章给大家分享的是有关Qt中如何获取桌面宽度高度的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

函数名

//桌面宽度高度
static int deskWidth();
static int deskHeight();

//程序文件名称+当前所在路径
static QString appName();
static QString appPath();

//初始化随机数种子
static void initRand();

函数体

int QUIHelper::deskWidth()
{
    //没有必要每次都获取,只有当变量为空时才去获取一次
    static int width = 0;
    if (width == 0) {
        width = qApp->desktop()->availableGeometry().width();
    }

    return width;
}

int QUIHelper::deskHeight()
{
    //没有必要每次都获取,只有当变量为空时才去获取一次
    static int height = 0;
    if (height == 0) {
        height = qApp->desktop()->availableGeometry().height();
    }

    return height;
}

QString QUIHelper::appName()
{
    //没有必要每次都获取,只有当变量为空时才去获取一次
    static QString name;
    if (name.isEmpty()) {
        name = qApp->applicationFilePath();
        QStringList list = name.split("/");
        name = list.at(list.count() - 1).split(".").at(0);
    }

    return name;
}

QString QUIHelper::appPath()
{
#ifdef Q_OS_ANDROID
    return QString("/sdcard/Android/%1").arg(appName());
#else
    return qApp->applicationDirPath();
#endif
}

void QUIHelper::initRand()
{
    //初始化随机数种子
    QTime t = QTime::currentTime();
    qsrand(t.msec() + t.second() * 1000);
}

感谢各位的阅读!关于“Qt中如何获取桌面宽度高度”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!