代码在Windows10、deepin5.18、MacOS11.0.1测试通过。
一、属性窗口的透明和鼠标事件穿透
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
// 透明窗口并且穿透鼠标事件
setTransEvents(true);
// 5秒后设置为不穿透
QTimer::singleShot(5000, this, [this]{
setTransEvents(false);
});
// 10秒后再次设置为穿透
QTimer::singleShot(10000, this, [this]{
setTransEvents(true);
});
}
void Widget::setTransEvents(bool bTrans)
{
#ifdef Q_OS_MACOS
setWindowFlag(Qt::FramelessWindowHint, true);
#endif
setAttribute(Qt::WA_TranslucentBackground, true);
setAttribute(Qt::WA_TransparentForMouseEvents, bTrans);
setWindowFlag(Qt::WindowTransparentForInput, bTrans);
setWindowFlag(Qt::WindowStaysOnTopHint, true);
if(!isVisible())