Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Click through windows

Click through windows

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 7.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vunnm
    wrote on last edited by
    #1

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

    D 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Looks like you want: widget->setAttribute(Qt::WA_TransparentForMouseEvents).

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi and welcome to devnet,

        Looks like you want: widget->setAttribute(Qt::WA_TransparentForMouseEvents).

        V Offline
        V Offline
        Vunnm
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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.
          };
          
          V 1 Reply Last reply
          1
          • mrjjM mrjj

            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.
            };
            
            V Offline
            V Offline
            Vunnm
            wrote on last edited by
            #5

            @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).

            mrjjM 1 Reply Last reply
            0
            • V Vunnm

              @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).

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @Vunnm

              Works here. (win 10)
              You can try my sample
              https://www.dropbox.com/s/v74riecj5zq1t3t/trans.zip?dl=0

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Vunnm
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0
                • V Vunnm

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

                  D Offline
                  D Offline
                  dschiller
                  wrote on last edited by dschiller
                  #8

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

                  06a6d3c2-adae-4e44-bc9e-2da1ee0c7bf6-image.png

                  And without:

                  e05632f8-29aa-470d-a1ff-3be218143f1a-image.png

                  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)
                  
                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SamiV123
                    wrote on last edited by
                    #9

                    On Win7 (at least) transparent windows are are feature of the Windows window compositor (Aero) and you need to enable transparency in Windows graphics/system settings.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved