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. at eventFilter(QObject *object, QEvent *event), can't get keyPress events when the MainWindow is hidden.
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 4.1k 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.
  • Miguel de SousaM Offline
    Miguel de SousaM Offline
    Miguel de Sousa
    wrote on last edited by Miguel de Sousa
    #1

    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.

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

      Hi and welcome to devnet,

      AFAIK you can't.

      What are you trying to achieve exactly ?

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

      1 Reply Last reply
      0
      • Miguel de SousaM Offline
        Miguel de SousaM Offline
        Miguel de Sousa
        wrote on last edited by
        #3

        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;
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Are you running that application on an embedded device ?

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

          1 Reply Last reply
          0
          • Miguel de SousaM Offline
            Miguel de SousaM Offline
            Miguel de Sousa
            wrote on last edited by
            #5

            Yes, I am :')

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

              Then is your application the only one running ?

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

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

              1 Reply Last reply
              0
              • Miguel de SousaM Offline
                Miguel de SousaM Offline
                Miguel de Sousa
                wrote on last edited by
                #7

                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.

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

                  Is this other application sharing the QWS server ?

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

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved