Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Center a QDialog in the middle of the MainWindow

[SOLVED] Center a QDialog in the middle of the MainWindow

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 17.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Note that, you should avoid using geometry() before the widget is shown. but you can call it in showEvent()

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Blizzard
        wrote on last edited by
        #3

        Please see my "post here":http://qt-project.org/forums/viewthread/17420/.

        Software Engineer | Aerospace & Defence

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vezprog
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved