窗口的透明需要在QWidget的构造函数里面调用QWidget的构造函数实现,而直接通过setWFlags则就算设置了也没效果。使用Qt::WStyle_NoBorder | Qt:WStyle_Customize。
在下面的代码当中,MainForm继承自MainFormBase,而MainFormBase是一个用Qt Designer生成的一个QWidget的子类。而这个MainForm我创建多几个效果也是一样的,每一个都没有任务栏项。而主程序使用QApplication就可以了。
MainForm::MainForm(QWidget
*
parent,
const
char
*
name)
:MainFormBase(parent, name,
Qt::WStyle_NoBorder | Qt::WStyle_Customize // <--- set flags here
)
{
// do not set flags here, otherwise it doesn't work
// the code below is to let the window have no taskbar entry
Atom net_wm_state_skip_taskbar=XInternAtom (qt_xdisplay(),
"_NET_WM_STATE_SKIP_TASKBAR", False);
Atom net_wm_state = XInternAtom (qt_xdisplay(),
"_NET_WM_STATE", False);
XChangeProperty (qt_xdisplay(), winId(), net_wm_state,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&net_wm_state_skip_taskbar, 1);
}
:MainFormBase(parent, name,
Qt::WStyle_NoBorder | Qt::WStyle_Customize // <--- set flags here
)
{
// do not set flags here, otherwise it doesn't work
// the code below is to let the window have no taskbar entry
Atom net_wm_state_skip_taskbar=XInternAtom (qt_xdisplay(),
"_NET_WM_STATE_SKIP_TASKBAR", False);
Atom net_wm_state = XInternAtom (qt_xdisplay(),
"_NET_WM_STATE", False);
XChangeProperty (qt_xdisplay(), winId(), net_wm_state,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&net_wm_state_skip_taskbar, 1);
}
另外,这几个头文件也是必须的:
#
include <X11/Xlib.h>
# include <X11/Xutil.h>
# include <X11/Xatom.h>
# include <X11/Xutil.h>
# include <X11/Xatom.h>
感谢为我提供思路的Stefan。
另外,这个网址的内容可以参考: http://standards.freedesktop.org/wm-spec/wm-spec-1.4.html#id2511406
后记:其实是我搞错了,上面的方法是X11的方法,而不是专门KWin的。如果是专门KWin,那么可以专门用这方法:
KWin::setState( winId(), NET::SkipTaskbar
||
NET::SkipPager )
在没有在taskbar显示的同时,我们还希望不在pager里面显示。
MainForm::MainForm(QWidget
*
parent,
const
char
*
name)
:MainFormBase(parent, name,
Qt::WStyle_NoBorder | Qt::WStyle_Customize // <--- set flags here
)
{
// do not set flags here, otherwise it doesn't work
// the code below is to let the window have no taskbar entry and not display in the pager
Atom net_wm_state_skip_taskbar=XInternAtom (qt_xdisplay(),
"_NET_WM_STATE_SKIP_TASKBAR", False);
Atom net_wm_state_skip_pager=XInternAtom (qt_xdisplay(),
"_NET_WM_STATE_SKIP_PAGER", False);
Atom net_wm_state = XInternAtom (qt_xdisplay(),
"_NET_WM_STATE", False);
XChangeProperty (qt_xdisplay(), winId(), net_wm_state,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&net_wm_state_skip_taskbar, 1);
XChangeProperty (qt_xdisplay(), winId(), net_wm_state,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&net_wm_state_skip_pager, 1);
}
:MainFormBase(parent, name,
Qt::WStyle_NoBorder | Qt::WStyle_Customize // <--- set flags here
)
{
// do not set flags here, otherwise it doesn't work
// the code below is to let the window have no taskbar entry and not display in the pager
Atom net_wm_state_skip_taskbar=XInternAtom (qt_xdisplay(),
"_NET_WM_STATE_SKIP_TASKBAR", False);
Atom net_wm_state_skip_pager=XInternAtom (qt_xdisplay(),
"_NET_WM_STATE_SKIP_PAGER", False);
Atom net_wm_state = XInternAtom (qt_xdisplay(),
"_NET_WM_STATE", False);
XChangeProperty (qt_xdisplay(), winId(), net_wm_state,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&net_wm_state_skip_taskbar, 1);
XChangeProperty (qt_xdisplay(), winId(), net_wm_state,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&net_wm_state_skip_pager, 1);
}
感谢Aaron J. Seigo和John Tapsell的指导。