Browse Source

Making the window be centralized

Rafael Arce Nazario 9 years ago
parent
commit
4d8bb29683
2 changed files with 33 additions and 1 deletions
  1. 9
    0
      main.cpp
  2. 24
    1
      mainwindow.cpp

+ 9
- 0
main.cpp View File

@@ -5,6 +5,7 @@
5 5
 #include "functions.h"
6 6
 #include <cassert>
7 7
 #include <QDebug>
8
+#include <QDesktopWidget>
8 9
 
9 10
 void testSort() {
10 11
     QString a = "AAA" , b = "BBB" , c = "CCC";
@@ -61,6 +62,14 @@ int main(int argc, char *argv[])
61 62
     srand(time(NULL));
62 63
     QApplication a(argc, argv);
63 64
     MainWindow w;
65
+
66
+    QDesktopWidget desktop;
67
+
68
+    int screenWidth, screenHeight;
69
+
70
+    screenWidth = desktop.screenGeometry().width();
71
+    screenHeight = desktop.screenGeometry().height();
72
+    w.move( ( screenWidth - w.width() )/2, ( screenHeight- w.height() ) /2);
64 73
     w.show();
65 74
 
66 75
 

+ 24
- 1
mainwindow.cpp View File

@@ -9,6 +9,7 @@
9 9
 #include <QtCore/qmath.h>
10 10
 #include <QMessageBox>
11 11
 #include "functions.h"
12
+#include <QDesktopWidget>
12 13
 
13 14
 MainWindow::MainWindow(QWidget *parent) :
14 15
     QMainWindow(parent),
@@ -39,6 +40,13 @@ MainWindow::MainWindow(QWidget *parent) :
39 40
 
40 41
     window = new secondwindow;
41 42
 
43
+    QDesktopWidget desktop;
44
+
45
+
46
+    window->move( ( desktop.screenGeometry().width() - window->width() )/2,
47
+                 ( desktop.screenGeometry().height()- window->height() ) /2);
48
+
49
+
42 50
     // We need to know whenever the second window is closed to show the
43 51
     // main window, or to hide when the second one is showed
44 52
     connect(window, SIGNAL(cerrado(bool)), this, SLOT(mostrar(bool)));
@@ -79,6 +87,14 @@ void MainWindow::mostrar(bool si){
79 87
 
80 88
         // Create the new window and connecting it again with the signal
81 89
         window = new secondwindow;
90
+
91
+        QDesktopWidget desktop;
92
+
93
+
94
+        window->move( ( desktop.screenGeometry().width() - window->width() )/2,
95
+                     ( desktop.screenGeometry().height()- window->height() ) /2);
96
+
97
+
82 98
         connect(window, SIGNAL(cerrado(bool)), this, SLOT(mostrar(bool)));
83 99
     }
84 100
     else hide();
@@ -534,6 +550,13 @@ void MainWindow::on_SortsButton_clicked()
534 550
     //notify when it was closed so we can shor the main-window
535 551
     window->setLayout(layout);
536 552
     mostrar(false);
553
+
554
+    QDesktopWidget desktop;
555
+
556
+
557
+    window->move( ( desktop.screenGeometry().width() - window->width() )/2,
558
+                 ( desktop.screenGeometry().height()- window->height() ) /2);
559
+
537 560
     window->show();
538 561
 }
539 562
 
@@ -721,6 +744,6 @@ void MainWindow::on_ZuluButton_clicked()
721 744
     //and show the new one. The new-window has a signal that
722 745
     //notify when it was closed so we can shor the main-window
723 746
     window->setLayout(layout);
747
+    mostrar(false);
724 748
     window->show();
725 749
 }
726
-