at eventFilter(QObject *object, QEvent *event), can't get keyPress events when the MainWindow is hidden.



  • I Have created a MainWindow class that inherits from QMainWindow. Inside this window, I want to get keyboard events while this window is visibile or invisible. In my program, this window could be visible or invisible, depending on the context.

    MainWindow.cpp:

    MainWindow::MainWindow()  {
        //..
        QCoreApplication::instance()->installEventFilter(this);
       //..
    }
    
        bool MainWindow::eventFilter(QObject *object, QEvent *event) {
            qDebug() << "print my event: " << event;
            if (event->type() == QEvent:KeyPress) {
                    doSomething();
        }
            return false;
        }
    

    This works perfectly fine while the MainWindow is visible. I can get all sort of QKeyEvents, printing them like this:

    print my event: QKeyEvent(KeyPress, key=0x34, text="4")
    print my event: QKeyEvent(keyRelease, key=0x34, text="4)
    

    However, when I hide the MainWindow with this->hide(), problems start to arise.
    When the window is hidden, I can't get KeyPress events, and the events are printed like this:

    print my event: QEvent(SockAct, oxbe971a9c, type = 50)
    

    That is, I expected to get KeyPress events, but all I can get are these Socket Activation events.

    Does anyone know how can I get KeyPress events when my MainWindow is hidden?
    I'm using Qt 4.8.7.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    AFAIK you can't.

    What are you trying to achieve exactly ?



  • HI, SGaist!

    What I want to achieve is an application that, by pressing a key combination from the keyboard, I could make a window invisible. When pressing again this combination, I could make it visible again.

    Notice that this window is frameless. In my project I only have a main.cpp, a mainwindow.cpp and another class with some inner logic for measuring the system memory usage.

    The UI logic is all inside the main.cpp and on the mainwindow.cpp.

    my main.cpp:

    #include "mainwindow.h"
    #include <QApplication>
    #include <QtGui>
    #include <stdlib.h>
    #include <QWSServer>
    #include <QDebug>
    
    int main(int argc2, char *argv2[])
    {
       //..
        QWSServer::setBackground(QBrush(Qt::transparent));
    
        QApplication a(argc, argv);
        MainWindow *window = new MainWindow();
    
        window->setWindowFlags(Qt::FramelessWindowHint);
        window->setFixedSize(QDesktopWidget().width()*0.83, QDesktopWidget().height()*0.33);
        window->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter,
                                               window->size(), a.desktop()->availableGeometry()));
    
            window->show();
        return a.exec();
    }
    

    my mainwindow.cpp:

    #include<QtGui>
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "memoryhandler.h"
    #include <QDebug>
    #include <QKeyEvent>
    
    MainWindow::MainWindow()  {
        memoryHandler = new MemoryHandler();
        setTimer();
        QCoreApplication::instance()->installEventFilter(this);
        initializeKeyboardState();
        initializeLayout();
        initializeLabels();
        updateLabels();
        addLabelsToLayout();
        boxLayout->setSizeConstraint(QLayout::SetMinimumSize);
        isApplicationVisible = true;
    }
    
    void MainWindow::setTimer()
    {
        timer = new QTimer(this);
        QObject::connect(timer, SIGNAL(timeout()), this, SLOT(updateLabels()));
        timer->start(1000);
    }
    
    void MainWindow::initializeKeyboardState()
    {
        keyboardState.isShortcutKeyPressed = false;
        keyboardState.is4KeyPressed = false;
        keyboardState.is2KeyPressed = false;
    }
    
    void MainWindow::initializeLayout()
    {
     //...
    }
    
    void MainWindow::updateLabels() {
    //...
    }
    
    void MainWindow::initializeLabels()
    {
    //...
    }
    
    void MainWindow::addLabelsToLayout()
    {
    //...
    }
    
    QString MainWindow::format(QString prefix, float value) {
        QString valueAsString = QString::number(value);
        return prefix + " = " + valueAsString + "Mb";
    }
    
    QString MainWindow::formatPercentage(QString prefix, float value) {
        QString valueAsString = QString::number(value);
        return prefix + " = " + valueAsString + "%";
    }
    
    
        bool MainWindow::getApplicationVisibility() {
            return isApplicationVisible;
        }
    
        bool MainWindow::eventFilter(QObject *object, QEvent *event) {
            qDebug() << "print the event: " << event;
            if (event->type() == QEvent::KeyPress) {
                    qDebug() << "gotcha";
        }
            return false;
        }
    
        void MainWindow::keyPressEvent(QKeyEvent *event) {
            qDebug() << "pressed, calling keyPressEvent";
            switch (event->key()) {
            case Qt::Key_Shift:
                keyboardState.isShortcutKeyPressed = true;
                break;
            case Qt::Key_4:
                keyboardState.is4KeyPressed = true;
                break;
            case Qt::Key_2:
                keyboardState.is2KeyPressed = true;
                break;
            }
            decideWindowVisibility();
        }
    
        void MainWindow::decideWindowVisibility()
        {
            if (isKeyboardStateTrue()) {
                if (isApplicationVisible) { this->hide(); }
                else { this->show(); }
                isApplicationVisible = !isApplicationVisible;
            }
        }
    
        bool MainWindow::isKeyboardStateTrue() {
            if (keyboardState.isShortcutKeyPressed &&
                    keyboardState.is4KeyPressed &&
                    keyboardState.is2KeyPressed)
                return true;
            return false;
        }
    
            void MainWindow::keyReleaseEvent(QKeyEvent *event) {
                qDebug() << "soltou, chamou keyReleaseEvent";
                switch (event->key()) {
                case Qt::Key_Escape:
                    keyboardState.isShortcutKeyPressed = false;
                    break;
                case Qt::Key_4:
                    keyboardState.is4KeyPressed = false;
                    break;
                case Qt::Key_2:
                    keyboardState.is2KeyPressed = false;
                    break;
                }
        }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        delete memoryHandler;
    }
    

  • Lifetime Qt Champion

    Are you running that application on an embedded device ?



  • Yes, I am :')


  • Lifetime Qt Champion

    Then is your application the only one running ?

    What do you do with it when your MainWindow gets hidden ?



  • Actually, there's a application running behind.

    In some moment, I intend to make the application behind start the foreground window, so the foreground window could display the memory usage for the user.

    I expected that both applications could catch events, but I'm having this problem with KeyPress events on this memory window thing, coming like Socket Activation events.


  • Lifetime Qt Champion

    Is this other application sharing the QWS server ?


Log in to reply
 

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