Getting MainWindow's absolute position problem

  • Hello there,
    I have an application where I created and then show a new dialog on top of my MainWindow. Without setting the geometry, the Dialog is placed in the middle of the MainWindow. What I was trying to do, is to get the MainWindow position and add offset to it and then pass the new X and Y position to Move method. Appereantly, if I understand this correctly, I cannot read the absolute position value of the MainWindow. please, take a look at this method:

    In the code above I am trying to make the new Dialog appear (show) in the right-down bottom of the MainWindow. Instead, the Dialog is showing with offset starting in the upper right corner of my monitor, not the MainWindow. Is there a way to get the MainWindow absolute position? In the end, what I am trying to do, is to show the dialog on the right-down Side of the MainWindow, like this (moved this by hand):

    I Would aprichiate all help!

  • @Bremenpl

    You can get the absolute position with this->pos(). So it is all in all pretty simple.
    auto p = pos();
    qDebug() << p;
    YourDialog dia(0, this);
    p += QPoint(this->width(), this->height());
    p -= QPoint(dia.width(), dia.height());

  • @Richard
    Thank you for fast answer. For my example, the code doesnt work the way it should. I dont know either its because I am using pointer to my Dialog object, but I wouldnt say so. Please take a look at this code I have commented it:

    It seems that pos() doesnt give absolute position... Could you reffer please? Heres a screen showing how the windows line up:

    Please notice that this.pos() and undoDialog->pos() both return (0, 0) when they are created. This means that the positions are at some point relative, not absolute.

    Edit: I have read the position of undoDialog when set like this (by hand):

    its (1112, 550), while the top left corner is (0, 0).
    It seems that I can get absolute position only after I execute show() method- If I make show() (of mainwindow) to execute before your code, then it works :).

  • Are you calling MainWindow::InitComponents() from the MainWindow's constructor?
    In this case you have to know that position and size is not yet initialized at this time...

    You could use a singleshot-Timer (with interval 0) to schedule MainWindow::InitComponents() to be run from the eventloop shortly after construction. Then the position should be initialized.
    (There's another function to add something to the eventloop that's a better solution, but I can't remember the name...)

    Oops, I see you found the solution yourself... Didn't read your post to the end, sorry ;-)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.