title: How to set programmatically QWidget in the center of the window in Qt?
body: <p>I have such code on Qt.</p>
  
  <pre><code>wdgCenter = new QWidget(wdgMain);
  wdgCenter-&gt;move(wdgCenter-&gt;parentWidget()-&gt;geometry().center());
  wdgCenter-&gt;resize(QSize(170, 115));
  
  welcomeLbl = new QLabel("Welcome to the Notes. Log in first.", wdgCenter);
  welcomeLbl-&gt;setGeometry(QRect(QPoint(0, 0), QSize(175, 20)));
  
  loginLbl = new QLabel("Login", wdgCenter);
  loginLbl-&gt;setGeometry(QRect(QPoint(0, 30), QSize(50, 20)));
  
  pswdLbl = new QLabel("Password", wdgCenter);
  pswdLbl-&gt;setGeometry(QRect(QPoint(0, 60), QSize(50, 20)));
  
  loginLine = new QLineEdit (wdgCenter);
  loginLine-&gt;setGeometry(QRect(QPoint(60, 30), QSize(110, 20)));
  
  passwordLine = new QLineEdit (wdgCenter);
  passwordLine-&gt;setGeometry(QRect(QPoint(60, 60), QSize(110, 20)));
  
  logInBtn = new QPushButton ("Log In", wdgCenter);
  logInBtn-&gt;setGeometry(QRect(QPoint(0, 90), QSize(50, 25)));
  
  createBtn = new QPushButton ("Create", wdgCenter);
  createBtn-&gt;setGeometry(QRect(QPoint(60, 90), QSize(50, 25)));
  </code></pre>
  
  <p>The result you can see below on the first picture.</p>
  
  <p><a href="https://i.stack.imgur.com/PqKEH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PqKEH.png" alt="default picture"></a></p>
  
  <p>I want to move my <code>wdgCenter</code> to the center of the window. I tried <code>QGridLayout</code> like this below.</p>
  
  <pre><code>QGridLayout* gLayout = new QGridLayout(wdgMain);
  
  gLayout-&gt;addWidget( welcomeLbl );
  gLayout-&gt;setAlignment(welcomeLbl,Qt::AlignHCenter);
  gLayout-&gt;addWidget( loginLbl, 1, 0  );
  gLayout-&gt;setAlignment(loginLbl,Qt::AlignHCenter);
  gLayout-&gt;addWidget( pswdLbl, 2, 0 );
  gLayout-&gt;setAlignment(pswdLbl,Qt::AlignHCenter);
  gLayout-&gt;addWidget( loginLine, 1, 1 );
  gLayout-&gt;setAlignment(loginLine,Qt::AlignHCenter);
  gLayout-&gt;addWidget( passwordLine, 2, 1 );
  gLayout-&gt;setAlignment(passwordLine,Qt::AlignHCenter);
  gLayout-&gt;addWidget( logInBtn, 3, 0 );
  gLayout-&gt;setAlignment(logInBtn,Qt::AlignHCenter);
  gLayout-&gt;addWidget( createBtn, 3, 1 );
  gLayout-&gt;setAlignment(createBtn,Qt::AlignHCenter);
  </code></pre>
  
  <p>And the result you can see on the next picture below.</p>
  
  <p><a href="https://i.stack.imgur.com/smu0J.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/smu0J.png" alt="enter image description here"></a></p>
  
  <p>I also tried <code>QHBoxLayout</code> and <code>QHBoxLayout</code> but they just draw me my objects in on row or in one column.</p>
  
  <p>In Qt Designer I did this with vertical and horizontal spacers like this:</p>
  
  <p><a href="https://i.stack.imgur.com/u4Olt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u4Olt.png" alt="enter image description here"></a></p>
  
  <p>But I need to do this programmatically.</p>
  
  <p>How to set my <code>wdgCenter</code> to The center?</p>