QT-Qt界面居中显示

 

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 // 桌面对象引入
 6 #include <QDesktopWidget>
 7 
 8 QT_BEGIN_NAMESPACE
 9 namespace Ui { class MainWindow; }
10 QT_END_NAMESPACE
11 
12 class MainWindow : public QMainWindow
13 {
14     Q_OBJECT
15 
16 public:
17     MainWindow(QWidget *parent = nullptr);
18     ~MainWindow();
19 
20 private:
21     Ui::MainWindow *ui;
22     // 桌面对象
23     QDesktopWidget *m_pDeskdop;
24 
25 };
26 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9     // 设置标题
10     setWindowTitle(QStringLiteral("Qt界面居中显示"));
11     // 设置界面大小
12     resize(400, 300);
13     // 为了测试,先把界面放在左100,顶200位置上。
14     move(100, 200);
15     // 桌面操作作
16     m_pDeskdop = QApplication::desktop();
17     move((m_pDeskdop->width() - this->width())/2, (m_pDeskdop->height() - this->height())/2);
18 }
19 
20 MainWindow::~MainWindow()
21 {
22     delete ui;
23 }
View Code

 

posted on 2020-07-15 09:44  疯狂delphi  阅读(599)  评论(0编辑  收藏  举报

导航