我正在使用 Qt,但我不知道如何将 QMainWindow 窗口居中。我写了这段代码,但它不起作用。提前致谢。
QRect screenGeometry = QApplication::desktop()->screenGeometry();
int x = (screenGeometry.width() - w->width()) / 2;
int y = (screenGeometry.height() - w->height()) / 2;
w->move(x, y); // w is a QMainWindow pointer
Run Code Online (Sandbox Code Playgroud)
我明白了:
这些功能已过时:
QApplication::desktop()->screenGeometry()
QApplication::desktop()->availableGeometry()
QDesktopWidget::screen()
Run Code Online (Sandbox Code Playgroud)
改用 QScreen:
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QScreen>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
move(screen()->geometry().center() - frameGeometry().center());
}
Run Code Online (Sandbox Code Playgroud)
谢谢大家。我已经使用这段代码解决了我的问题。
w->setFixedSize(400, 400);
int width = w->frameGeometry().width();
int height = w->frameGeometry().height();
QDesktopWidget wid;
int screenWidth = wid.screen()->width();
int screenHeight = wid.screen()->height();
w->setGeometry((screenWidth/2)-(width/2),(screenHeight/2)-(height/2),width,height);
w->show();
Run Code Online (Sandbox Code Playgroud)
我明白了:
| 归档时间: |
|
| 查看次数: |
2805 次 |
| 最近记录: |