如何在QT中居中一个QDialog?

10

我有这个示例代码:

QDialog *dialog = new QDialog(this);
QPoint dialogPos = dialog->mapToGlobal(dialog->pos());
QPoint thisPos = mapToGlobal(this->pos());
dialog->exec();

但是对话框没有居中显示在它的父级上。非常感谢。

更新:

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

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();

}

但我收到的是:

在此输入图片描述


http://www.qtcentre.org/threads/43802-Centering-child-window-in-parent https://dev59.com/v3XZa4cB1Zd3GeqPAv5o - Max Go
4个回答

21

我在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);
    }
}

希望这可以帮到你。

编辑

修复最近Qt版本发出的弃用警告:

#include <QScreen>
#include <QWidget>
#include <QGuiApplication>

inline void CenterWidgets(QWidget *widget, QWidget *host = Q_NULLPTR) {
    if (!host)
        host = widget->parentWidget();

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

3
这段代码仅适用于第一个屏幕。如果您想将其应用于其他屏幕,请将屏幕的geometry中的x和y坐标值添加到代码中的x和y坐标值上以进行居中处理。 - goug
@goug 你是什么意思?能给个例子吗?谢谢。 - Mecanik

4

我想在这里发布自己的解决方案。@CapelliC的解决方案虽然可行,但自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));

希望能够帮到你。

2

您需要更改QDialog的几何形状:

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

move()函数会根据父级进行移动,因此不需要映射到全局。

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

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

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


谢谢,我更新了我的问题,对话框没有正常工作。 :( - Omar Murcia

-1

我认为这是Qt4的一个bug。我在Ubuntu上使用Qt4时,它不会将父窗口居中。

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

您也可以使用move()函数来到达您想要的位置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接