2

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?

Community
  • 1
  • 1
student
  • 352
  • 2
  • 15
  • I cannot test it right now so can you try `setVisible(true)` instead of show? – Hayt Nov 08 '16 at 13:16
  • I have all the mecanism except this property to stay in the same spot. This is the problem I can not find a solution. – student Nov 08 '16 at 13:17
  • @drescherjm it should do this though. It is defined that way: http://doc.qt.io/qt-5/qwidget.html#hide – Hayt Nov 08 '16 at 13:21
  • You could possibly override `changeEvent(QEvent *event)` and save and restore the position when the dialog is hidden or shown. – drescherjm Nov 08 '16 at 13:21
  • @drescherjm well it's only with a Dialog or subclasses. not with widgets. – Hayt Nov 08 '16 at 13:23
  • Is there any way to go around it? I can not change QDialog with QWidget. – student Nov 08 '16 at 13:24
  • That explains it. I mostly use widgets when I do similar because I don't need / want them to be modal. – drescherjm Nov 08 '16 at 13:24
  • Try overriding `showEvent`. – thuga Nov 08 '16 at 13:24
  • @student have you tried `setVisible(true)` instead of calliing `show`? – Hayt Nov 08 '16 at 13:24
  • Yes. Same behavior. – student Nov 08 '16 at 13:28
  • @Hayt `show()` is actually calling `setVisible(true)`. See the [doc](http://doc.qt.io/qt-5/qwidget.html#show) – rocambille Nov 08 '16 at 13:30
  • @wasthishelpful yeah I was not sure though if there are some differences. But seeing that `QDialog` has overridden `showEvent` it is probably somewhere in there. – Hayt Nov 08 '16 at 13:32
  • Years after this, I had the same problem and it turns out that indeed overriding the showEvent - just a dummy with no code in it - prevents the QDialog's showEvent to be called, which indeed explicitly centers the dialog over the parents window every time before it shows the dialog. – ajabo Mar 25 '18 at 20:54

1 Answers1

6

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.

rocambille
  • 15,398
  • 12
  • 50
  • 68
  • This seems to work fine. Apparently I can not override the setVisible function but I manage to make a function of my own that modifies this. – student Nov 08 '16 at 14:10