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. Using event filters as a class
Forum Updated to NodeBB v4.3 + New Features

Using event filters as a class

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.8k Views 1 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.
  • M Offline
    M Offline
    marlenet15
    wrote on last edited by
    #1

    On my mainwindow I have 6 QLineEdits, 2 QTextEdits, and a virtual keyboard I created. Currently I installed an even filter on each edit field so that when it has focus, the keyboard will recognize which edit field to how the characters being inputted by the keyboard. The following code is currently on my mainwindow and it does work.

    void MainWindow::obtainEditFields() 
    {
    _lineedits = ui->_stackedWidget->currentWidget()->findChildren<QLineEdit *>();
    _textedits = ui->_stackedWidget->currentWidget()->findChildren<QTextEdit *>();
    
    for (int i=0; i<_lineedits.count(); i++)
        _lineedits.at(i)->installEventFilter(this);
    
    for (int i=0; i<_textedits.count(); i++)
        _textedits.at(i)->installEventFilter(this);
    }
    
    bool MainWindow::eventFilter(QObject *target, QEvent *)
    {
    if(target->objectName().contains("TextEdit"))
    {
        for (int i=0; i<_textedits.count(); i++)
        {
            if(_textedits.at(i)->hasFocus())
                ui->_keyboard->m_focusItem = _textedits.at(i);
        }
    }
    else
    {
        for (int i=0; i<_lineedits.count(); i++)
        {
            if(_lineedits.at(i)->hasFocus())
                ui->_keyboard->m_focusItem = _lineedits.at(i);
        }
    }
    return false;
    }
    

    However, I would like to create a separate class for this specific code. I created a class called EditFields.cpp which is now being called by mainwindow. Obviously right now is not working since the variables lineEdits and textEdits are not being recognized on the eventFilter. Is there a way to make them recognized?

    #include "editfields.h"
    #include <QEvent>
    #include <QFocusEvent>
    
    EditFields::EditFields() : QObject()
    {
        keyboard = new Keyboard;
    }
    
    void EditFields::getEditFields(QList<QLineEdit *> lineEdits, QList<QTextEdit *> textEdits)
    {
    _lineEdit_count = lineEdits.count();
    _textEdit_count = textEdits.count();
    
        for (int i=0; i<_lineEdit_count; i++)
    {
        lineEdits.at(i)->installEventFilter(this);
    }
    
    for (int i=0; i<_textEdit_count; i++)
    {
        textEdits.at(i)->installEventFilter(this);
    }
    }
    
    bool EditFields::eventFilter(QObject *target, QEvent *event)
    {
    if(target->objectName().contains("TextEdit"))
    {
        for (int i=0; i<_textEdit_count; i++)
        {
             if(textEdits.at(i)->hasFocus())
                keyboard->m_focusItem = textEdits.at(i);
        }
    }
    else
    {
        for (int i=0; i<_lineEdit_count; i++)
        {
             if(lineEdits.at(i)->hasFocus())
                keyboard->m_focusItem = lineEdits.at(i);
        }
    }
    return false;
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You would need to keep a copy of these list in your class. However, why not create a QPlatformInputContext ? See this article. That will avoid the custom filtering.

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

      M 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        You would need to keep a copy of these list in your class. However, why not create a QPlatformInputContext ? See this article. That will avoid the custom filtering.

        M Offline
        M Offline
        marlenet15
        wrote on last edited by
        #3

        @SGaist I am using the keyboard as a widget which is currently in a stackedwidget. The reasoning is because the keyboard has to be included with the application I am creating.

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

          If you take a look at the article you'll see that it proposes to use a custom keyboard widget (a bit like yours) which will be included with the application.

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

          M 1 Reply Last reply
          0
          • SGaistS SGaist

            If you take a look at the article you'll see that it proposes to use a custom keyboard widget (a bit like yours) which will be included with the application.

            M Offline
            M Offline
            marlenet15
            wrote on last edited by
            #5

            @SGaist I was reading it and I can't quite understand what is the purpose of QPlatformInputContext?

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

              That's the piece that makes your keyboard appear when you click on an input widget and ensure the event is sent to the appropriate widget

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

              M 1 Reply Last reply
              0
              • SGaistS SGaist

                That's the piece that makes your keyboard appear when you click on an input widget and ensure the event is sent to the appropriate widget

                M Offline
                M Offline
                marlenet15
                wrote on last edited by
                #7

                @SGaist Ohhh my keyboard is always there regardless whether the QLineEdit is clicked or not

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

                  You can keep it visible, the thing is that with it you won't need to handle yourself the active input widget.

                  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

                  • Login

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