[SOLVED] Center a QDialog in the middle of the MainWindow
-
I am trying to center a custom QDialogBox in the middle of the MainWindow it was spawned from...
here's what I have tried. After creating the dialog I call a function the calculates the positioning on the screen with respect to the MainWindows geometry..
@
void MainWindow::actionClicked()
{
CDialog *dialog = new CDialog();
setDisabled(true); // set the main window disabled
setWindowOpacity(.5); // change opacity
dialog->centerDialogWindow(geometry().center().x(), geometry().center().y());
dialog->show();
}
@where in the dialog class
@
void CDialog::centerDialogWindow(int x, int y)
{
move((x - (geometry().center().x())), (y - (geometry().center().y())));
}
@I have tried other things as well, but this one makes the most sense to me, but is unsuccessful (ends up to the upper left of the main window screen). Is there an easier way to do this? There is a bunch of old threads on this on other forums but non have had any successful conclusions...
In a nutshell, I want the center of my dialog box to be the same point as the center of the mainwindow.
Thanks!
-
Note that, you should avoid using geometry() before the widget is shown. but you can call it in showEvent()
-
Please see my "post here":http://qt-project.org/forums/viewthread/17420/.
-
I think setting the size policy of the Dialog window is irrelevant to this issue...I have the horizontal and vertical set to fixed since I don't want to allow re-sizing of the dialog window. Usually the size policy is used when using a QLayout of some sort. From my understanding, this will not "center" the window.
1+1=2, I will try to override the show event and just set some public variables to the x and y values instead of calling the function, before the show and see if this works.
Does the window size/geometry not get calculated until the showEvent() is called? I am adding widgets and what not in my constructor so I just figured it would be calculated once the constructor has completed.
Thanks!
Edit: I replaced the code I posted above into the show event and it worked perfectly. Thank you again.