Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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

    Hi
    in what way does setGeometry not work ?
    http://doc.qt.io/qt-5/qwidget.html#geometry-prop
    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>
    
    window->setGeometry(
        QStyle::alignedRect(
            Qt::LeftToRight,
            Qt::AlignCenter,
            window->size(),
            qApp->desktop()->availableGeometry()
        )
    );
    


  • Solved. Thanks so much @mrjj.


Log in to reply