How to keep a QWidget (or QDialog) centered to its parent widget?

后端 未结 2 1096
醉酒成梦
醉酒成梦 2021-01-14 15:50

I want to keep a QWidget (or QDialog) centered to its parent widget. Is it really required to connect to signals of the parent widget or is there an easier way (something to

2条回答
  •  梦谈多话
    2021-01-14 16:35

    I solved it myself and just to answer my own question if someone will find it helpful.

    Keep it in the center:

    void MyMainWindow::moveEvent(QMoveEvent* event)
    {
        const QPoint global = this->mapToGlobal(rect().center());
        waitDialog->move(global.x() - waitDialog->width() / 2, global.y() - waitDialog->height() / 2);
    }
    

    ... where waitDialog is an instance of my "Wait..." indicator. This needs to override the moveEvent method of the parent (here MyMainWindow).

提交回复
热议问题