Question

Windows 7 has this functionality where if a window is dragged to the side of the screen, it is maximized to take half of it. My problem is - I am trying to implement restore for widget's size and position and when Windows 7 "maximizes" the widget, qt still returns its position and size as if it was still shown normally, i.e - completely incorrect position and size.

Is there any control over this in qt5? I can't find it anywhere in the docs and its strange

Was it helpful?

Solution

This is essentially what I ended up doing. "framegeometry" part at the bottom is necessary to compensate for widget's borders it seems.

QT += gui-private

is needed in the .pro file for the code below to work

#include <WinUser.h>
#include <qpa/qplatformnativeinterface.h>
static QWindow* windowForWidget(const QWidget* widget)
{
    QWindow* window = widget->windowHandle();
    if (window)
        return window;
    const QWidget* nativeParent = widget->nativeParentWidget();
    if (nativeParent)
        return nativeParent->windowHandle();
    return 0;
}
#include <QWindow>
static HWND getHWNDForWidget(const QWidget* widget)
{
    QWindow* window = ::windowForWidget(widget);
    if (window && window->handle())
    {
        QPlatformNativeInterface* natInterface = QGuiApplication::platformNativeInterface();
        return static_cast<HWND>(natInterface->nativeResourceForWindow(QByteArrayLiteral("handle"), window));
    }
    return 0;
}
void SaveSize(QWidget* w)
{
    QSize size;
    QPoint pos;
    RECT pRect = { 0 };
    HWND hwnd = getHWNDForWidget(w);
    GetWindowRect(hwnd, &pRect);
    auto left = w->frameGeometry().left();
    auto right = w->frameGeometry().right();
    auto width = w->width();
    pos.setX(pRect.left);
    pos.setY(pRect.top);
    size.setWidth(pRect.right - pRect.left - (right - left - width));
    size.setHeight(pRect.bottom - pRect.top);
    //.... the rest of the code
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top