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
  • 相关阅读:
    解决 SQL Server Profiler 跟踪[不断]出现检索数据
    Linq表达式开窍
    CSS3——动画效果
    MongoDB学习与BUG解答
    MongoDB 客户端 MongoVue
    Memcached——分布式缓存
    WRONGTYPE Operation against a key holding the wrong kind of value
    Redis——分布式简单使用
    HTML5——播放器
    HTML5——行走日记
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/13303825.html
Copyright © 2011-2022 走看看