c++qt

How to maintain position of a QDialog on hide/show


I have a QDialog where I have a checkable button witch opens/close (hide/show) another window (the window is also a QDialog).

My problem is I wish to maintain the position of the second QDialog when I hide and after open it (Example: I move the QDialog in a corner of the screen and when I hide and then show it the QDialog needs to be in the same corner).

At this moment it seems to be restored at the initial position. I found a similar article about it at similar but in my case I have two separate windows and I can move the second QDialog over the screen. Did anyone meet with this problem?


Solution

  • AFAIK this works without any code: position isn't changed when showing/hiding a widget.

    Anyway, you can store the position of your QDialog through its geometry:

    // save geometry
    
    QRect geometry = my_dialog->geometry();
    my_dialog->hide();
    
    // restore geometry
    
    my_dialog->show();
    my_dialog->setGeometry(geometry);
    

    Note that the geometry is relative to the parent: I assume here your QDialog is modeless.