How to make QWidget translucent for events?

I saw similar questions, but did not answer what I needed. I want an invisible widget that lives on top of my entire application (no problem here). I want this widget to capture events so that I can print material about them, record them, whatever. I currently have an event filter that does this just fine. Then I want this event to go through everything behind the widgets. For example, if I try to press a button, an invisible widget should notice that the press was pressed at that place, and then in fact you need to press the button. Can this be done in a simple way, or will I have to write code to simulate all events under invisible widgets?

+5
source share
1 answer

From all the information that you revealed in the comments, I suggest you filter the event, as discussed earlier, and then use QCoreApplication :: sendEvent to move the necessary events to an invisible widget. He will then distribute the event accordingly to his children.

EDIT: OK, here is a brief example that includes an event filter based on QObject that will filter events for the widget, if the event is a mouse event, it will remain for the widget to process and print the output, if the event is a key event, it will be filtered and not will be redirected back to the widget:

Event Filter Class:

    class EventInfo : public QObject {
    Q_OBJECT
public:
    explicit EventInfo(QObject *parent = 0) : QObject(parent) {}

    bool eventFilter(QObject *, QEvent *e) {
        if (e->type() == QEvent::MouseButtonRelease){
            qDebug() << "click event not filtered";
            return false;
        }
        if (e->type() == QEvent::KeyRelease) {
            QKeyEvent *event = static_cast<QKeyEvent *>(e);
            if (event) qDebug() << "key" << event->key() << "filtered";
            return true;
        }
        return false;
    }
};

Widget:

class Widget : public QWidget {
    Q_OBJECT

public:
    Widget(QWidget *parent = 0) : QWidget(parent) {}

protected:
    void mouseReleaseEvent(QMouseEvent *e) {
        qDebug() << "widget clicked at position" << e->pos();
    }

    void keyReleaseEvent(QKeyEvent *e) {
        qDebug() << "pressed key" << e->key();
    }
};

main.cpp:

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    Widget w;
    EventInfo info;
    w.installEventFilter(&info);
    w.show();    
    return a.exec();
}

Testing the output to show that keyboard events are filtered out and mouse click events are sent to the widget:

click event not filtered
widget clicked at position QPoint(352,230) 

key 70 filtered 

click event not filtered
widget clicked at position QPoint(405,163) 

key 87 filtered
+3

All Articles