Nominate our 2022 Qt Champions!

Native title bar size for proper window placement ?

  • Hi everyone!

    How do I properly calculate the initial window position so that my windows title bar isn't cut off when spawning it at the top?

    Or is there a way to do this using Qt functions already?


  • Lifetime Qt Champion


    What exactly are you experiencing ?

    You can position it a 0.0 or at the center of the screen.

  • The thing is, we desire to spawn the window exactly at the position centered on top of the parent. But we don't want to cross the screen boundaries. Therefore we want to have the frame aligned with the screen edges.

    The problem now is that using setFramePosition(0,0) sometimes sets it a few pixels too much to the right of the left screen border.

  • Lifetime Qt Champion

    Why not use your main window geometry to place your other widget ?

  • This is the behaviour we want:


    This is the behaviour we see:


    Isn't there a way to get the desired outcome with a normal Qt function that just handles these cases for us?
    Like a flag RespectScreenBorders

    Edit: Please note that the desired rectangles in the second picture is what we get. I forgot to change the labels to "Result"

  • Lifetime Qt Champion

    No there's not. You can put your windows outside of the screen on purpose or you could be placing them in a secondary screen.

    Can you show the code you use to move the widgets around ?

  • our current code calculates the new window geometry() using availableGeometry().
    then we setGeometry() to resize and reposition the window in its constructor. This doesn't take the window titlebar into account though.

    So we just add 32 to our min y in order to make space for the titlebar. this works just fine on windows 10. but we want to support any platform equally.

    Using setFramePosition() works well in that regard, unless we set it to (0,0). That's when we see the behavior in the picture above to the top left. And having it at the bottom obviously lets it go too far down into the taskbar.

    the best solution would be to only use setFramePosition when our calculated new position is (newpos < frameheight). But we can't get the frame margins without opening the window first and the mainwindow doesn't provide these margins.

  • Lifetime Qt Champion

    You should use frameGeometry.

  • I suppose the window has to be created before the frame is known.
    But we need the frame geometry beforehand.

  • Lifetime Qt Champion

    The geometry of the frame is not known before the widget is shown.

    The showEvent method might be of help.

  • Thanks, that seems to work. Although it's weird that frameMargins().left() returns a number way too high on my scaling (13). The top() is correct with 58 on my setup.
    This makes the frame still show the horizontal displacement. I'll have to just force it to 0 and use a combination of setGeometry and setPosition to work around this.

Log in to reply