Search code examples
c++qtqmlqt-quick

QML - main window position on start (screen center)


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


Solution

  • 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).