Important: Please read the Qt Code of Conduct -

setGeometry vs Resize+Move

  • Hi,
    I have a dialog, that I need to resize 2 or 3 times. The dialog should always be centered, so I use resize , and move:

    // within the dialog class (a slot)
    this->move(x() - ((newWidth-width())/2) , y() - ((newHeight-height())/2));
    this->resize(newWidth, newHeight);

    This code works, but when I use setGeometry with the same parameters it won't work. I need setGeometry to work because I want to animate the resizing.
    That's it. Thanks for your help.

  • Lifetime Qt Champion

    in what way does setGeometry not work ?
    This is relative to parent coordinates
    However, Move docs says
    "If the widget is a window, the position is that of the widget on the desktop, including its frame."
    And since you have a dialog, it might apply.

    Anyway, you can center a dialog to center of screen with

    #include <QStyle>
    #include <QDesktopWidget>

  • Solved. Thanks so much @mrjj.

Log in to reply