8

Recently I am working on a Qt Gui application with c++ and I need to align my window to top left corner when I run the application. (It should open on the top left corner by default).

Is there any way I can do this on the code or by Qt designer?

I would be really glad if someone could help.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
BUY
  • 705
  • 1
  • 11
  • 30

1 Answers1

7

Supposing you have a main window, move it to the top left corner of the primary screen rectangle (get the screen object, and its geometry, from the QApplication instance):

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow w;

    QRect screenrect = a.primaryScreen()->geometry();
    w.move(screenrect.left(), screenrect.top());
    w.show();

    return a.exec();
}
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35