qt5 窗口启动居中显示

 

main.cpp里面修改

#include "mainwindow.h"
#include <QApplication>
#include <QScreen>
#include <QDesktopWidget>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    //w.show();


    //移动窗体到屏幕中央
    QRect rect = w.frameGeometry();
    QDesktopWidget desktop;
    QPoint centerPoint = desktop.availableGeometry().center();
    rect.moveCenter(centerPoint);
    w.move(rect.topLeft());

    w.show();

    return a.exec();
}

qt6.0

//移动窗体到屏幕中央
    QRect rect = w.frameGeometry();
//    QDesktopWidget desktop;
    QScreen* screen = QGuiApplication::primaryScreen();
    QPoint centerPoint = screen->availableGeometry().center();
    rect.moveCenter(centerPoint);
    w.move(rect.topLeft());
 
    w.show();

 

posted @ 2023-11-09 15:09  txwtech  阅读(93)  评论(0编辑  收藏  举报