QML - 开始时的主窗口位置(屏幕中心)

2022-01-19 00:00:00 qt qml qt-quick c++

如何执行以下操作:我想在开始时在中央屏幕上显示我的主窗口.

How I can do following: I’d like show my main window on start on the center screen.

推荐答案

你需要 setGeometry 在您显示它之前在您的顶级小部件上.我能想到的最简单的方法是通过 <代码>QDesktopWidget.试试下面的例子(创建一个 QPushButton,在不同屏幕上移动小部件时按下它),你就会明白我的意思了:

You'll need to setGeometry on your top-level widget before you show it. The easiest way I can think of to work out what geometry you need is via QDesktopWidget. Try the example below (create a QPushButton, press it while moving the widget around various screens) and you'll see what I mean:

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{   
  ui->setupUi(this);
  connect(ui->pushButton, SIGNAL(released()), this, SLOT(ButtonPressed()));
}

MainWindow::~MainWindow()
{
  delete ui;
}

void MainWindow::ButtonPressed()
{
  qDebug() << QApplication::desktop()->screenCount();
  qDebug() << QApplication::desktop()->screenNumber();
  qDebug() << QApplication::desktop()->screenGeometry(this);
}

应该相当简单地从那里拿出一个通用版本来计算用户的中心屏幕(如果它存在的话).

Should be reasonably simple from there to come up with a generic version that works out a user's center screen (if it exists).

相关文章