3

I search a way to put a QWidget window of a Qt application always on the current desktop/workspace. When I change the virtual desktop, I need my window to be automatically visible on the new current virtual desktop.

I search a solution that works at minimum on gnome, kde on linux and mac os.

I think the first step is to detect when virtual desktop is change, I don't know if Qt have a API for that or if I need to implement it for each desktop solution.

Thanks in advance for all help.

4

2 回答 2

4

在 X11 下,您应该_NET_WM_DESKTOPwindow 属性设置为0xFFFFFFFF. 我怀疑没有 Qt API,所以你必须用#ifdefs 检查 X11 并调用适当的 X 函数(不,我不知道它是哪一个)。

于 2013-05-27T16:56:01.640 回答
3

感谢 Jan Kundrát 的帮助(先前的评论https://stackoverflow.com/a/16777496/1045832)。

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
}

把它放在你的 .pro 上

unix:!macx {
    LIBS += -lX11
}

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);

把它放在你的 .pro 上

macx {
    LIBS += -lobjc
}
于 2013-06-02T12:21:24.223 回答