Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML - main window position on start (screen center)

QML - main window position on start (screen center)

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 4 Posters 22.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AntyaDev
    wrote on last edited by
    #1

    Hello all,

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

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Punit_Tak
      wrote on last edited by
      #2

      Hello AntyaDev,
      If you are using C++ also then you can put these line in main.cpp file.

      @void setCenterOfApplication(QWidget* widget)
      {
      QSize size = widget->sizeHint();
      QDesktopWidget* desktop = QApplication::desktop();
      int width = desktop->width();
      int height = desktop->height();
      int mw = size.width();
      int mh = size.height();
      int centerW = (width/2) - (mw/2);
      int centerH = (height/2) - (mh/2);
      widget->move(centerW, centerH);
      }@

      Punit Tak

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chaos6
        wrote on last edited by
        #3

        this is another one
        put these line in main.cpp file

        @void center(QWidget &widget ,int WIDTH , int HEIGHT)
        {
        int x, y;
        int screenWidth;
        int screenHeight;

        QDesktopWidget *desktop = QApplication::desktop();

        screenWidth = desktop->width();
        screenHeight = desktop->height();

        x = (screenWidth - WIDTH) / 2;
        y = (screenHeight - HEIGHT) / 2;

        widget.setGeometry(x, y, WIDTH, HEIGHT);

        }@

        http://smashcs.wordpress.com/

        1 Reply Last reply
        0
        • F Offline
          F Offline
          francomartins
          wrote on last edited by
          #4

          this option is a more efficient!

          @
          void YourClass::centerWidget(QWidget *w, bool useSizeHint)
          {
          if(w->isFullScreen())
          return;

          QSize size;
          if(useSizeHint)
              size = w->sizeHint();
          else
              size = w->size();
          
          QDesktopWidget *d = QApplication::desktop();
          int ws = d->width();   // returns screen width
          int h = d->height();  // returns screen height
          int mw = size.width();
          int mh = size.height();
          int cw = (ws/2) - (mw/2);
          int ch = (h/2) - (mh/2);
          w->move(cw,ch);
          

          }
          @

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved