Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Position QWidget fully in screen, including window decoration



  • I understand the difference between geometry() and frameGeometry(). What I need is a "setFrameGeometry", in order to position a window (borders, title bar, and all) nicely inside a screen.

    Lacking a setFrameGeometry, I looked for properties that would help me calculate the proper geometry (e.g. how high is the title bar, how wide are the borders), but didn't see anything.

    My next try was to calculate the correct geometry to set by calculating offsets between frameGeometry and geometry. But at least inside the constructor, the values for frameGeometry don't make sense, so no help there.

    Any ideas?


  • Qt Champions 2019

    @Asperamanca So, you actually want to maximise the window, right?



  • @jsulm No. I want to manually set the size and position, because e.g. I want the window to stretch two screens. That cannot be done via maximize.


  • Qt Champions 2019

    @Asperamanca I don't think you can get the size of the window decorations using Qt as those are drawn by the window manager of your OS, not Qt.



  • This code works for me. Still have to look whether it's portable, but so far, I see nothing indicating the contrary:

    void CWidget::setFrameGeometry(const QRect rect)
    {
        (void) winId();
        const auto* const pWindow = windowHandle();
        QMargins margins;
        if (pWindow)
        {
            margins = pWindow->frameMargins();
        }
    
        const auto adjustedRect = rect.adjusted(margins.left(),
                                                margins.top(),
                                                margins.right(),
                                                margins.bottom());
    
        setGeometry(adjustedRect);
    }
    

    I just wonder why they didn't provide this as a standard QWidget method


Log in to reply