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


  • Lifetime Qt Champion

    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:
    0_1505123472466_fenetre.png


  • Qt Champions 2016

    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.
    };
    


  • @mrjj I simply copy/paste your code but I still get the same result: a black window and the click don't go through the window (I try it on both Windows and linux).


  • Qt Champions 2016

    @Vunnm

    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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.