zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Perforce笔记
    Lumia 800 无法正常开机
    Windows service 中出现Set ServiceLogin 对话框
    华为要求七千员工先辞职再竞岗 补偿费超10亿
    BLOG新址:http://longware.spaces.live.com
    家装(2)
    解脱
    论持久战
    有感于软件项目测试
    THE POEM AS A GIFT FOR MY GF'S BIRTHDAY
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/13303825.html
Copyright © 2011-2022 走看看