QT 获取屏幕分辨率以及设置窗体大小

1,在Windows下可以使用 GetSystemMetrics(SM_CXSCREEN);GetSystemMetrics(SM_CYSCREEN) 获取。

2,在Linux下可以使用XDisplayWidth ;XDisplayHeight ()获取。
3,在QT中呢?很多人说是 QApplication::desktop()->width();QApplication::desktop()->height(); 这个方法对于单显示器模式当然没有问题。但是对于多显示器,特别是使用了扩展桌面的就会有问题了。今天上午仔细看了QDesktopWidget的帮助,需要使用QApplication::desktop()->screenGeometry();这个函数有好几个重载的版本,意思都一样。该函数返回一个QRect,这个QRect的宽和高就是所在Screen的分辨率。获取方法如下:

void GetScreenInfo()
{
    QDesktopWidget* desktopWidget = QApplication::desktop();
    //获取可用桌面大小
    QRect deskRect = desktopWidget->availableGeometry();
    //获取设备屏幕大小
    QRect screenRect = desktopWidget->screenGeometry();

    g_nActScreenX = screenRect.width();
    g_nActScreenY = screenRect.height();
    //g_nActScreenX = deskRect.width();
    //g_nActScreenY = deskRect.height();

    //获取系统设置的屏幕个数(屏幕拷贝方式该值为1)
    g_nScreenCount = desktopWidget->screenCount();
}说到这里,顺便标记以下多屏幕设置成拷贝方式时,获取的屏幕的个数是一个,只有设置成扩展时才返回多个。

打印屏幕分辨率和个数信息:

void printscreeninfo()

{
    QDesktopWidget *dwsktopwidget = QApplication::desktop();
    QRect deskrect = dwsktopwidget->availableGeometry();
    QRect screenrect = dwsktopwidget->screenGeometry();
    QDesktopWidget *dwsktopwidget = QApplication::desktop();
    QRect deskrect = dwsktopwidget->availableGeometry();
    QRect screenrect = dwsktopwidget->screenGeometry();
    int scrcount = dwsktopwidget->screenCount();
    qCritical("screenrect.w==%s\n",qPrintable(QString::number(screenrect.width())));
    qCritical("screenrect.h==%s\n",qPrintable(QString::number(screenrect.height())));
    qCritical("deskrect.w==%s\n",qPrintable(QString::number(deskrect.width())));
    qCritical("deskrect.h==%s\n",qPrintable(QString::number(deskrect.height())));
    qCritical("scrcount==%s\n",qPrintable(QString::number(scrcount)));

}

4. 设置窗体大小

void setGeometry(int x, int y, int w, int h)

void setGeometry(const QRect &)

void resize(int w, int h)

void resize(const QSize &)

猜你喜欢

转载自blog.csdn.net/technologyleader/article/details/82150069
今日推荐