10

I have this example code:

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

But the Dialog is not centered on his parent. Thanks in advance.

UPDATE:

I'm calling Dialog from constructor in MainWindow:

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

}

But I get is:

enter image description here

Omar Murcia
  • 547
  • 2
  • 11
  • 26
  • http://www.qtcentre.org/threads/43802-Centering-child-window-in-parent http://stackoverflow.com/questions/18385916/how-to-keep-a-qwidget-or-qdialog-centered-to-its-parent-widget – Max Go Feb 19 '17 at 06:59
  • Possible duplicate of [How to keep a QWidget (or QDialog) centered to its parent widget?](http://stackoverflow.com/questions/18385916/how-to-keep-a-qwidget-or-qdialog-centered-to-its-parent-widget) – Max Go Feb 19 '17 at 07:00

4 Answers4

21

I have this code in 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);
    }
}

Hope it helps

edit

fix the deprecation warning issued from recent Qt versions:

#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);
    }
}
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • 3
    This code works only on the first screen. If you're trying to center on a different screen, you have to add screenGeometry.x() and screenGeometry.y() to x and y. – goug Sep 27 '20 at 23:35
  • @goug What do you mean? Show example? Thanks. – Mecanik Feb 18 '23 at 17:19
4

Thought I would post my own solution here. @CapelliC's solution works, but is deprecated since Qt5.11. Infact, the documentation says the QDesktopWidget class is obsolete.

Solution (which is a litte crude) is to use the QGuiApplication::screenAt()

Context: class inheriting QMainWindow, but can can extend for any 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));

Hope it helps.

CybeX
  • 2,060
  • 3
  • 48
  • 115
2

You have to change the geometry of the QDialog:

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

The move() function moves respect the parent, so it is not necessary to map to global.

On constructor the position and size of parent are not set yet. You can try executing the dialog in a separate method or, if needed on constructor, try with something like

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

It will be shown on the next iteration of the event loop.

cbuchart
  • 10,847
  • 9
  • 53
  • 93
-1

I think it's a Qt4 bug. I used Qt4 on Ubuntu and it doesn't respect the parent widget center.

However, when I use Qt5, It seems to work fine.

You can also use move() to arrive to your position.

Isma
  • 14,604
  • 5
  • 37
  • 51
cryfeifei
  • 29
  • 4