Can't unset Qt::WA_TransparentForMouseEvents attribute
-
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.
-
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.
@BahadirCan I forgot to mention this is my ui
Basically, the frame is used to give radius to the window and I've set the background colors of the widgets using rgba values.
-
Hi and welcome to devnet,
Can you try to hide and show the widget again after you toggle the attribute ?
-
Hi and welcome to devnet,
Can you try to hide and show the widget again after you toggle the attribute ?
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. -
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.@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... -
@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...@jsulm
Yes you are right, for some reason I did not think it would cause any problems but anyways, I tried thisvoid 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.