Click through windows
-
Hi,
How do I create a transparent window with mouse events going through the window? Actually I try to set transparency with this in the constructor:
setAttribute(Qt::WA_TranslucentBackground, true);
But it only works on linux, under Windows I get a black background and I still don't know how to make mouse click going through my window.
I hope someone will know how to fix it :) -
Hi and welcome to devnet,
Looks like you want:
widget->setAttribute(Qt::WA_TransparentForMouseEvents)
. -
@SGaist I already try it:
here is my code:
#include "transparentwindow.h" TransparentWindow::TransparentWindow() : QWidget() { setFixedSize(300, 150); m_b= new QPushButton("Ok", this); //setWindowFlag(Qt::X11BypassWindowManagerHint); setAttribute(Qt::WA_NoSystemBackground, true); setAttribute(Qt::WA_TranslucentBackground, true); setAttribute(Qt::WA_TransparentForMouseEvents); }
header of the file:
#ifndef TRANSPARENTWINDOW_H #define TRANSPARENTWINDOW_H #include <QObject> #include <QWidget> #include <QPushButton> class TransparentWindow : public QWidget { public: TransparentWindow(); protected: QPushButton *m_b; }; #endif // TRANSPARENTWINDOW_H and my main.cpp:
#include <iostream>
#include <QApplication>
#include <transparentwindow.h>int main(int argc, char**argv)
{
QApplication app(argc, argv);
TransparentWindow f;
f.show();return app.exec();
}
but here is the result:
-
Hi
Being drawn transparently or have mouse event passthrough is not the same.
Anyway, i used this to have a 100% invisible window and clicks went through it.
(win 10, not tried on linux)class Overlay : public QWidget { Q_OBJECT public: explicit Overlay ( QWidget* parent = 0 ) : QWidget ( parent ) { setPalette ( Qt::transparent ); setAttribute ( Qt::WA_TransparentForMouseEvents ); } protected: void paintEvent ( QPaintEvent* event ) {} // draw nothing ( i did draw some stuff) not 100% sure its needed. };
-
Works here. (win 10)
You can try my sample
https://www.dropbox.com/s/v74riecj5zq1t3t/trans.zip?dl=0 -
Well your project don't work for my, perhaps because I am using a 64 bits Windows 7 on virtual machine and compiling my project in 32 bits. But anyway I find a solution on another forum, here.
here the constructor of my transparent and click-through window:
MyWindows::MyWindows() : QWidget() { //setWindowFlag(Qt::X11BypassWindowManagerHint); setAttribute(Qt::WA_TranslucentBackground, true); setAttribute(Qt::WA_TransparentForMouseEvents); setWindowFlags(Qt::FramelessWindowHint); setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); //we can add Qt::Tool //setFixedSize(300,300); }
so thanks everyone for your help!
-
@Vunnm What I also do - I know it's Python code, but that changed a lot for me.
self.setStyleSheet('background: transparent')
With setting the style sheet background to
transparent
:And without:
Here the full code of my
UI Init
method:from PyQt5.QtWidgets import QVBoxLayout from PyQt5.QtCore import Qt from ..Scene import Scene from ..GraphicsView import GraphicsView from ..properties import Properties def initUI(self): """Initialize the user interface of the window.""" props = Properties() self.setGeometry(0, 0, int(self.screen_.width(0)), int(self.screen_.height(0))) self.setStyleSheet('background: transparent') self.setWindowFlag(Qt.WindowType.FramelessWindowHint) # Only necessary on Windows self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground) self.layout = QVBoxLayout() self.layout.setContentsMargins(0, 0, 0, 0) self.setLayout(self.layout) props.scene = Scene() self.view = GraphicsView(props.scene.grScene, self) self.layout.addWidget(self.view)
It has come to my attention that the proper arrangement of Window Flags holds significance as well.
Having the flags in this order:
self.setWindowFlag(Qt.WindowType.FramelessWindowHint) self.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents)
Makes the
WA_TransparentForMouseEvents
useless and it doesn't show any effect.Whereas this order works and I can click trough the window:
self.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents) self.setWindowFlag(Qt.WindowType.FramelessWindowHint)