Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Click through windows

    General and Desktop
    5
    9
    5903
    Loading More Posts
    • 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
      Vunnm last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 2
        • V
          Vunnm @SGaist last edited by

          @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 Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by 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 1 Reply Last reply Reply Quote 1
            • V
              Vunnm @mrjj last edited by

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

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @Vunnm last edited by mrjj

                @Vunnm

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

                1 Reply Last reply Reply Quote 0
                • V
                  Vunnm last edited by

                  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 Reply Quote 0
                  • D
                    dschiller @Vunnm last edited by dschiller

                    @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 Reply Quote 0
                    • S
                      SamiV123 last edited by

                      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 Reply Quote 0
                      • First post
                        Last post