如何在QT中居中QDialog?

时间:2017-02-19 06:36:40

标签: c++ qt qmainwindow qdialog

我有这个示例代码:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

    this->panelInferior = new WidgetTabsInferior;
    this->acciones = new Acciones(this);

    crearAcciones();
    crearBarraMenu();
    crearToolbar();
    crearTabsEditor();
    crearArbolDir();
    crearDockWindows();
    crearStatusBar();

    setWindowIcon(QIcon(":imgs/logo.png"));

    connect(this->pestanasEditor , SIGNAL(currentChanged(int)),this,SLOT(cambioTab(int)));

    this->dialogo = new AcercaDe(this);
    this->dialogo->move(x() + (width() - dialogo->width()) / 2,
                 y() + (height() - dialogo->height()) / 2);
    this->dialogo->show();
    this->dialogo->raise();
    this->dialogo->activateWindow();

}

但是Dialog并不以他的父母为中心。提前谢谢。

更新:

我在MainWindow中从构造函数调用Dialog:

{
  "name": "AuFormsEx",
  "type": "project:application",
  "platform": {
    "id": "aspnetcore",
    "displayName": "ASP.NET Core",
    "output": "scripts",
    "index": "index.html",
    "baseUrl": "scripts"
  },
  "transpiler": {
    "id": "typescript",
    "displayName": "TypeScript",
    "fileExtension": ".ts",
    "dtsSource": [
      "./typings/**/*.d.ts",
      "./custom_typings/**/*.d.ts"
    ],
    "source": "src/**/*.ts"
  },
  "markupProcessor": {
    "id": "none",
    "displayName": "None",
    "fileExtension": ".html",
    "source": "src/**/*.html"
  },
  "cssProcessor": {
    "id": "none",
    "displayName": "None",
    "fileExtension": ".css",
    "source": "src/**/*.css"
  },
  "editor": {
    "id": "visualstudio",
    "displayName": "Visual Studio"
  },
  "unitTestRunner": {
    "id": "none",
    "displayName": "None"
  },
  "paths": {
    "root": "src",
    "resources": "src/resources",
    "elements": "src/resources/elements",
    "attributes": "src/resources/attributes",
    "valueConverters": "src/resources/value-converters",
    "bindingBehaviors": "src/resources/binding-behaviors"
  },
  "build": {
    "targets": [
      {
        "id": "aspnetcore",
        "displayName": "ASP.NET Core",
        "output": "scripts",
        "index": "index.html",
        "baseUrl": "scripts"
      }
    ],
    "loader": {
      "type": "require",
      "configTarget": "vendor-bundle.js",
      "includeBundleMetadataInConfig": "auto",
      "plugins": [
        {
          "name": "text",
          "extensions": [
            ".html",
            ".css"
          ],
          "stub": true
        }
      ]
    },
    "options": {
      "minify": "stage & prod",
      "sourcemaps": "dev & stage"
    },
    "bundles": [
      {
        "name": "app-bundle.js",
        "source": [
          "[**/*.js]",
          "**/*.{css,html}"
        ]
      },
      {
        "name": "vendor-bundle.js",
        "prepend": [
          "node_modules/bluebird/js/browser/bluebird.core.js",
          "node_modules/requirejs/require.js"
        ],
        "dependencies": [
          "aurelia-binding",
          "aurelia-bootstrapper",
          "aurelia-dependency-injection",
          "aurelia-event-aggregator",
          "aurelia-framework",
          "aurelia-history",
          "aurelia-history-browser",
          "aurelia-loader",
          "aurelia-loader-default",
          "aurelia-logging",
          "aurelia-logging-console",
          "aurelia-metadata",
          "aurelia-pal",
          "aurelia-pal-browser",
          "aurelia-path",
          "aurelia-polyfills",
          "aurelia-route-recognizer",
          "aurelia-router",
          "aurelia-task-queue",
          "aurelia-templating",
          "aurelia-templating-binding",
          "text",
          {
            "name": "aurelia-templating-resources",
            "path": "../node_modules/aurelia-templating-resources/dist/amd",
            "main": "aurelia-templating-resources"
          },
          {
            "name": "aurelia-templating-router",
            "path": "../node_modules/aurelia-templating-router/dist/amd",
            "main": "aurelia-templating-router"
          },
          {
            "name": "aurelia-testing",
            "path": "../node_modules/aurelia-testing/dist/amd",
            "main": "aurelia-testing",
            "env": "dev"
          }
        ]
      }
    ]
  }
}

但我得到的是:

enter image description here

4 个答案:

答案 0 :(得分:6)

我在github

中有这段代码
inline void CenterWidgets(QWidget *widget, QWidget *host = 0) {
    if (!host)
        host = widget->parentWidget();

    if (host) {
        auto hostRect = host->geometry();
        widget->move(hostRect.center() - widget->rect().center());
    }
    else {
        QRect screenGeometry = QApplication::desktop()->screenGeometry();
        int x = (screenGeometry.width() - widget->width()) / 2;
        int y = (screenGeometry.height() - widget->height()) / 2;
        widget->move(x, y);
    }
}

希望有所帮助

答案 1 :(得分:1)

您必须更改QDialog的几何图形:

dialog->move(x() + (width() - dialog->width()) / 2,
             y() + (height() - dialog->height()) / 2);

move()函数移动尊重父级,因此不必映射到全局。

在构造函数上,尚未设置父级的位置和大小。您可以尝试在单独的方法中执行对话框,或者,如果构造函数需要,可以尝试使用类似

的方法
QTimer::singleShot(0, [=]() {
  // ... your dialog code
});

它将在事件循环的下一次迭代中显示。

答案 2 :(得分:1)

以为我会在这里发布自己的解决方案。 @CapelliC的solution可以使用,但是从Qt5.11开始不推荐使用。实际上,文档显示QDesktopWidget类已过时。

解决方案这是一种轻质原油)将使用QGuiApplication::screenAt()

上下文:类继承了QMainWindow,但可以扩展为任何QWidget

// Get current screen size
QRect rec = QGuiApplication::screenAt(this->pos())->geometry();

// Using minimum size of window
QSize size = this->minimumSize();

// Set top left point
QPoint topLeft = QPoint((rec.width() / 2) - (size.width() / 2), (rec.height() / 2) - (size.height() / 2));

// set window position
setGeometry(QRect(topLeft, size));

希望有帮助。

答案 3 :(得分:-1)

我认为这是一个Qt4错误。我在Ubuntu上使用了Qt4,它并不尊重父小部件中心。

然而,当我使用Qt5时,似乎工作正常。

您也可以使用move()到达您的位置。