浏览代码

Making the window be centralized

父节点
当前提交
4d8bb29683
共有 2 个文件被更改,包括 33 次插入1 次删除
  1. 9
    0
      main.cpp
  2. 24
    1
      mainwindow.cpp

+ 9
- 0
main.cpp 查看文件

5
 #include "functions.h"
5
 #include "functions.h"
6
 #include <cassert>
6
 #include <cassert>
7
 #include <QDebug>
7
 #include <QDebug>
8
+#include <QDesktopWidget>
8
 
9
 
9
 void testSort() {
10
 void testSort() {
10
     QString a = "AAA" , b = "BBB" , c = "CCC";
11
     QString a = "AAA" , b = "BBB" , c = "CCC";
61
     srand(time(NULL));
62
     srand(time(NULL));
62
     QApplication a(argc, argv);
63
     QApplication a(argc, argv);
63
     MainWindow w;
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
     w.show();
73
     w.show();
65
 
74
 
66
 
75
 

+ 24
- 1
mainwindow.cpp 查看文件

9
 #include <QtCore/qmath.h>
9
 #include <QtCore/qmath.h>
10
 #include <QMessageBox>
10
 #include <QMessageBox>
11
 #include "functions.h"
11
 #include "functions.h"
12
+#include <QDesktopWidget>
12
 
13
 
13
 MainWindow::MainWindow(QWidget *parent) :
14
 MainWindow::MainWindow(QWidget *parent) :
14
     QMainWindow(parent),
15
     QMainWindow(parent),
39
 
40
 
40
     window = new secondwindow;
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
     // We need to know whenever the second window is closed to show the
50
     // We need to know whenever the second window is closed to show the
43
     // main window, or to hide when the second one is showed
51
     // main window, or to hide when the second one is showed
44
     connect(window, SIGNAL(cerrado(bool)), this, SLOT(mostrar(bool)));
52
     connect(window, SIGNAL(cerrado(bool)), this, SLOT(mostrar(bool)));
79
 
87
 
80
         // Create the new window and connecting it again with the signal
88
         // Create the new window and connecting it again with the signal
81
         window = new secondwindow;
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
         connect(window, SIGNAL(cerrado(bool)), this, SLOT(mostrar(bool)));
98
         connect(window, SIGNAL(cerrado(bool)), this, SLOT(mostrar(bool)));
83
     }
99
     }
84
     else hide();
100
     else hide();
534
     //notify when it was closed so we can shor the main-window
550
     //notify when it was closed so we can shor the main-window
535
     window->setLayout(layout);
551
     window->setLayout(layout);
536
     mostrar(false);
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
     window->show();
560
     window->show();
538
 }
561
 }
539
 
562
 
721
     //and show the new one. The new-window has a signal that
744
     //and show the new one. The new-window has a signal that
722
     //notify when it was closed so we can shor the main-window
745
     //notify when it was closed so we can shor the main-window
723
     window->setLayout(layout);
746
     window->setLayout(layout);
747
+    mostrar(false);
724
     window->show();
748
     window->show();
725
 }
749
 }
726
-