Keep a application window always on current desktop on linux and mac

谁说我不能喝 提交于 2019-12-01 20:39:58

Under X11, you are supposed to set the _NET_WM_DESKTOP window property to 0xFFFFFFFF. I suspect there's no Qt API for that, so you'll have to litter the code with #ifdefs checking for X11 and call an appropriate X's function (nope, I don't know which one it is).

Julien DAUPHANT

Thanks to Jan Kundrát for his help (Previous comment https://stackoverflow.com/a/16777496/1045832 ).

Solution for linux X11 :

#ifdef Q_WS_X11 //only define on Qt 4.X 
#include <QX11Info> //Only on Qt 4.X , return expected in Qt 5.1
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#endif

YourWidget::YourWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::YourWidget)
{

#ifdef Q_WS_X11 //only define on Qt 4.X 
    unsigned long data = 0xFFFFFFFF;
    XChangeProperty (QX11Info::display(),
                     winId(),
                     XInternAtom(QX11Info::display(), "_NET_WM_DESKTOP", False),
                     XA_CARDINAL,
                     32,
                     PropModeReplace,
                     reinterpret_cast<unsigned char *>(&data), // all desktop
                     1);
#endif
}

Put this on your .pro

unix:!macx {
    LIBS += -lX11
}

Solution for macos X :

#include <objc/objc-runtime.h>

WId windowObject = this->winId();
objc_object * nsviewObject = reinterpret_cast<objc_object *>(windowObject);
objc_object * nsWindowObject = objc_msgSend(nsviewObject, sel_registerName("window"));
int NSWindowCollectionBehaviorCanJoinAllSpaces = 1 << 0;
objc_msgSend(nsWindowObject, sel_registerName("setCollectionBehavior:"), NSWindowCollectionBehaviorCanJoinAllSpaces);

Put this on your .pro

macx {
    LIBS += -lobjc
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!