Transparency Behaviour Different in OSX/WIndows and Linux



  • I am attempting to write a screenshare application using Qt5. It works well on OSX and Windows but the same code does not work in the same way under Linux.

    The following files give a simple demonstation of my problem. I have a frameless, transparent, always-on-top QWidget on which I have a QPushButton. When I click the QPushButton I expect
    the handler to be called. When an input event occurs anywhere else in the QWidget I would like the window beneath the widget to receive the
    event.

    window.h:

    #include <QWidget>
    
    class Window : public QWidget
    {
    Q_OBJECT
    
    public:
        Window();
    
    protected:
        void paintEvent(QPaintEvent *pEvent);
    
    private slots:
        void buttonPressed();
    };
    

    window.cpp:

    #include <QDebug>
    #include <QPainter>
    #include <QColor>
    #include <QPen>
    #include <QPushButton>
    #include <QVBoxLayout>
    
    #include "window.h"
    
    Window::Window() {
        setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    
        setAttribute(Qt::WA_TranslucentBackground, true);
        setStyleSheet("background: transparent;");
    
        QVBoxLayout *pLayout = new QVBoxLayout();
    
        QPushButton *pButton = new QPushButton("Press Me");
        pButton->setStyleSheet("background-color: green; border: none;");
        connect(pButton, &QPushButton::pressed, this, &Window::buttonPressed);
        pLayout->addWidget(pButton);
    
        QWidget *pView = new QWidget(this, Qt::WindowTransparentForInput);
        pLayout->addWidget(pView);
    
        setLayout(pLayout);
    
        resize(300, 300);
    }
    
    void Window::paintEvent(QPaintEvent *pEvent) {
        int borderWidth = 5;
        QPainter painter(this);
    
        QColor colour("red");
    
        QPen pen(colour, borderWidth);
        painter.setPen(pen);
        painter.drawRect(rect());
    
        QWidget::paintEvent(pEvent);
    }
    
    void Window::buttonPressed() {
        qDebug() << "PRESSED BUTTON";
    }
    

    main.cpp:

    #include <QApplication>
    #include <QDebug>
    #include "window.h"
    
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
    
        Window window;
    
        window.show();
    
        return app.exec();
    }
    

    This works well on OSX and Windows. However, if I use exactly the same code on Linux, the QPushButton works but any input events in the other areas of the widget do not get passed through to the lower windows. If I add the Qt::WindowTransparentForInput window flag then on all OSs it is not possible to interact with the QPushButton.

    Is there any way in which the OSX/Windows functionlity can be achieved on Linux? I would be wiling to consider native code if necessary!

    Many thanks for looking.


Log in to reply
 

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