Elad Weiss
Elad Weiss

Reputation: 4002

Qt - Is there a way to check if the window is marked always-on-top (linux)?

Is there a way to check if the Qt window is marked always-on-top (by the user)? I would like to check that on closeEvent() and save it for the next time the user opens the window.

P.S: I checked the windowFlags hoping that Qt.WindowStaysOnTopHint flag will be set, but the flags don't seem to be affected.

Upvotes: 1

Views: 285

Answers (1)

p-a-o-l-o
p-a-o-l-o

Reputation: 10047

Using xlib, the needed window state hint can be checked calling the XGetWindowProperty function.

Check requisites first, e.g. sudo apt-get install libx11-dev.

In the pro file, link xlib and require the x11extras qt module.

QT += x11extras
LIBS += -lX11

This is a working example, a function that returns true if the passed-in widget pointer points to a always-on-top window:

#include <X11/Xlib.h>
#include <QtX11Extras/QX11Info>

bool isAlwaysOnTop(QWidget * widget)
{
    Atom atr;
    int  afr;
    unsigned long items;
    unsigned long bytes;
    unsigned char *data;

    Display * display = QX11Info::display();
    Atom property = XInternAtom(display, "_NET_WM_STATE", False);
    if(XGetWindowProperty(display, widget->winId(), property, 0L, 1L, False, 4, &atr, &afr, &items, &bytes, &data) == 0)
    {
        Atom abv = XInternAtom(display, "_NET_WM_STATE_ABOVE", False);
        Atom res = reinterpret_cast<Atom *>(data)[0];
        return (res==abv);
    }
    return false;
}

It can be used from inside the widget closeEvent:

void Form::closeEvent(QCloseEvent *)
{
    qDebug() << isAlwaysOnTop(this);
}

Upvotes: 1

Related Questions