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. Can't unset Qt::WA_TransparentForMouseEvents attribute
Forum Updated to NodeBB v4.3 + New Features

Can't unset Qt::WA_TransparentForMouseEvents attribute

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 906 Views 2 Watching
  • 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.
  • B Offline
    B Offline
    BahadirCan
    wrote on last edited by
    #1

    I'm trying to make a window that will stay on top and transparent to mouse events but I want to toggle this transparency to mouse events with a keyboard shortcut.

    //mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QMouseEvent>
    #include <QPoint>
    #include <QShortcut>
    
    #include <QDebug>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindow * ui;
        QPoint startPos;
        QShortcut* focusShortcut;
    
        void configureTitleBar();
        void makeConnections();
    
        bool focusable = false;
    
    protected:
        void mousePressEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        void toggleFocus();
    };
    #endif // MAINWINDOW_H
    
    
    //mainwindow.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        setAttribute(Qt::WA_TranslucentBackground);
        setAttribute(Qt::WA_TransparentForMouseEvents, focusable);
        setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    
        focusShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_L),this);
    
        makeConnections();
        configureTitleBar();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::configureTitleBar()
    {
        ui->windowTitle->setText("My Notes");
    }
    
    void MainWindow::makeConnections()
    {
        connect(ui->pushButtonClose, &QPushButton::released, this, &MainWindow::close);
        connect(ui->pushButtonMinimize, &QPushButton::released, this, &MainWindow::showMinimized);
        connect(focusShortcut, &QShortcut::activated, this, &MainWindow::toggleFocus);
    }
    
    void MainWindow::mousePressEvent(QMouseEvent *event)
    {
        startPos = event->pos();
        QWidget::mousePressEvent(event);
    }
    
    void MainWindow::mouseMoveEvent(QMouseEvent *event)
    {
        QPoint delta = event->pos() - startPos;
            QWidget * w = window();
            if(w)
                w->move(w->pos() + delta);
            QWidget::mouseMoveEvent(event);
    }
    
    void MainWindow::toggleFocus()
    {
        ui->textEdit->append("shortcut triggered");
        focusable = !focusable;
        setAttribute(Qt::WA_TransparentForMouseEvents, focusable);
    }
    

    The shortcut is activated but the attribute is not toggled.

    What am I doing wrong?

    Thanks for any help.

    B 1 Reply Last reply
    0
    • B BahadirCan

      I'm trying to make a window that will stay on top and transparent to mouse events but I want to toggle this transparency to mouse events with a keyboard shortcut.

      //mainwindow.h
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QMouseEvent>
      #include <QPoint>
      #include <QShortcut>
      
      #include <QDebug>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      private:
          Ui::MainWindow * ui;
          QPoint startPos;
          QShortcut* focusShortcut;
      
          void configureTitleBar();
          void makeConnections();
      
          bool focusable = false;
      
      protected:
          void mousePressEvent(QMouseEvent *event);
          void mouseMoveEvent(QMouseEvent *event);
          void toggleFocus();
      };
      #endif // MAINWINDOW_H
      
      
      //mainwindow.cpp
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          setAttribute(Qt::WA_TranslucentBackground);
          setAttribute(Qt::WA_TransparentForMouseEvents, focusable);
          setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
      
          focusShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_L),this);
      
          makeConnections();
          configureTitleBar();
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::configureTitleBar()
      {
          ui->windowTitle->setText("My Notes");
      }
      
      void MainWindow::makeConnections()
      {
          connect(ui->pushButtonClose, &QPushButton::released, this, &MainWindow::close);
          connect(ui->pushButtonMinimize, &QPushButton::released, this, &MainWindow::showMinimized);
          connect(focusShortcut, &QShortcut::activated, this, &MainWindow::toggleFocus);
      }
      
      void MainWindow::mousePressEvent(QMouseEvent *event)
      {
          startPos = event->pos();
          QWidget::mousePressEvent(event);
      }
      
      void MainWindow::mouseMoveEvent(QMouseEvent *event)
      {
          QPoint delta = event->pos() - startPos;
              QWidget * w = window();
              if(w)
                  w->move(w->pos() + delta);
              QWidget::mouseMoveEvent(event);
      }
      
      void MainWindow::toggleFocus()
      {
          ui->textEdit->append("shortcut triggered");
          focusable = !focusable;
          setAttribute(Qt::WA_TransparentForMouseEvents, focusable);
      }
      

      The shortcut is activated but the attribute is not toggled.

      What am I doing wrong?

      Thanks for any help.

      B Offline
      B Offline
      BahadirCan
      wrote on last edited by
      #2

      @BahadirCan I forgot to mention this is my ui
      83bc4423-9053-4145-85f2-a3c9184312a7-image.png

      Basically, the frame is used to give radius to the window and I've set the background colors of the widgets using rgba values.

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

        Hi and welcome to devnet,

        Can you try to hide and show the widget again after you toggle the attribute ?

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

        B 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          Can you try to hide and show the widget again after you toggle the attribute ?

          B Offline
          B Offline
          BahadirCan
          wrote on last edited by
          #4

          @SGaist

          void MainWindow::hideShow()
          {
              hide();
              QThread::sleep(1);
              show();
          }
          
          

          tried it like this but it didn't work.
          I also tried calling update() but it didn't also work.
          I'm feeling like I'm missing something so simple.

          jsulmJ 1 Reply Last reply
          0
          • B BahadirCan

            @SGaist

            void MainWindow::hideShow()
            {
                hide();
                QThread::sleep(1);
                show();
            }
            
            

            tried it like this but it didn't work.
            I also tried calling update() but it didn't also work.
            I'm feeling like I'm missing something so simple.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @BahadirCan said in Can't unset Qt::WA_TransparentForMouseEvents attribute:

            hide();
            QThread::sleep(1);
            show();

            This is not the proper way to do that!
            sleep() blocks the UI!
            Please use QTimer to call show() after some time...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            B 1 Reply Last reply
            0
            • jsulmJ jsulm

              @BahadirCan said in Can't unset Qt::WA_TransparentForMouseEvents attribute:

              hide();
              QThread::sleep(1);
              show();

              This is not the proper way to do that!
              sleep() blocks the UI!
              Please use QTimer to call show() after some time...

              B Offline
              B Offline
              BahadirCan
              wrote on last edited by
              #6

              @jsulm
              Yes you are right, for some reason I did not think it would cause any problems but anyways, I tried this

              void MainWindow::toggleFocus()
              {
                  focusable = !focusable;
                  ui->textEdit->append("setting transparent ");
                  ui->textEdit->append(focusable ? "true" : "false");
                  setAttribute(Qt::WA_TransparentForMouseEvents, focusable);
                  hide();
                  QTimer::singleShot(1000, this, &MainWindow::hideShow);
              }
              
              void MainWindow::hideShow()
              {
                  show();
              }
              
              

              still no luck.

              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