其他分享
首页 > 其他分享> > qt启动自动调整窗口宽高按照显示屏幕宽高比例显示并且居中显示

qt启动自动调整窗口宽高按照显示屏幕宽高比例显示并且居中显示

作者:互联网

代码如下:

#include <mainwindow.h>

#include <QApplication>
#include <QLocale>
#include <QTranslator>
#include <QDesktopWidget>
#include <QDebug>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTranslator translator;
    const QStringList uiLanguages = QLocale::system().uiLanguages();
    for (const QString &locale : uiLanguages) {
        const QString baseName = "modbus_" + QLocale(locale).name();
        if (translator.load(":/i18n/" + baseName)) {
            a.installTranslator(&translator);
            break;
        }
    }
    MainWindow w;
    // 获取屏幕宽高
    QRect deskRect = QApplication::desktop()->availableGeometry();
    int width = deskRect.right() * 0.7;
    int height = deskRect.bottom() * 0.6;
    qDebug() << "height:"<<height;
    // 挪动
//    w.move(deskRect.x(),deskRect.y());
    // 设置宽高并且居中显示
    w.setGeometry((deskRect.right() - width) / 2,(deskRect.bottom() - height) / 2,width,height);
    w.show();
    return a.exec();
}

 

标签:显示,const,qt,int,宽高,translator,uiLanguages,include
来源: https://www.cnblogs.com/qianxiaoPro/p/15459164.html