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. What should I return in eventFilter?
Forum Updated to NodeBB v4.3 + New Features

What should I return in eventFilter?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 890 Views
  • 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.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by
    #1

    Hi,

    For example I have mainWindow and myTextEdit ( inherits QTextEdit ) on it. I install in mainWindow eventFilter for mainWindow and myTextEdit like this:

    mainWindow constructor:
        textEdit = new MyTextEdit(this);
        textEdit->installEventFilter(this);
        installEventFilter(this);
    

    And of course I have eventFilter in mainWindow:

    bool MainWindow::eventFilter(QObject *object, QEvent *event)
    {
        if( object == textEdit )
        {
            if(event->type() == QEvent::Type::KeyPress) 
            {
                qInfo()<<"some Text";
                event->ignore();
                return true;
            }
            else
            {
                return ...... // I don't know what I should return here
            }
        }
        else if(object == this)
        {
            if(event->type() == QEvent::Type::KeyPress) 
            {
                qInfo()<<"some Text";
                event->ignore();
                return true;
            }
            else
            {
                return QMainWindow::eventFilter(object, event);
            }
        }
    }
    

    In (object == this ( mainWindow )) I return "QMainWindow::eventFilter(object, event);" because MainWindow inherits QMainWindow, so I think I should return in (object == textEdit) "QTextEdit::eventFilter(object, event);". But I can't. I get errors:

    1. call to non-static member function without an object argument
    2. eventFilter is protected member of QAbstractScrollArea
    jsulmJ eyllanescE 2 Replies Last reply
    0
    • Q qwe3

      Hi,

      For example I have mainWindow and myTextEdit ( inherits QTextEdit ) on it. I install in mainWindow eventFilter for mainWindow and myTextEdit like this:

      mainWindow constructor:
          textEdit = new MyTextEdit(this);
          textEdit->installEventFilter(this);
          installEventFilter(this);
      

      And of course I have eventFilter in mainWindow:

      bool MainWindow::eventFilter(QObject *object, QEvent *event)
      {
          if( object == textEdit )
          {
              if(event->type() == QEvent::Type::KeyPress) 
              {
                  qInfo()<<"some Text";
                  event->ignore();
                  return true;
              }
              else
              {
                  return ...... // I don't know what I should return here
              }
          }
          else if(object == this)
          {
              if(event->type() == QEvent::Type::KeyPress) 
              {
                  qInfo()<<"some Text";
                  event->ignore();
                  return true;
              }
              else
              {
                  return QMainWindow::eventFilter(object, event);
              }
          }
      }
      

      In (object == this ( mainWindow )) I return "QMainWindow::eventFilter(object, event);" because MainWindow inherits QMainWindow, so I think I should return in (object == textEdit) "QTextEdit::eventFilter(object, event);". But I can't. I get errors:

      1. call to non-static member function without an object argument
      2. eventFilter is protected member of QAbstractScrollArea
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @qwe3 said in What should I return in eventFilter?:

      QTextEdit::eventFilter(object, event);

      Well, this of course cannot work because eventFilter is not static and MainWindow is not a subclass of QTextEdit.
      You can simply return false, so it will go to your textEdit.
      https://doc.qt.io/qt-5/qobject.html#eventFilter
      "In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false."

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • Q qwe3

        Hi,

        For example I have mainWindow and myTextEdit ( inherits QTextEdit ) on it. I install in mainWindow eventFilter for mainWindow and myTextEdit like this:

        mainWindow constructor:
            textEdit = new MyTextEdit(this);
            textEdit->installEventFilter(this);
            installEventFilter(this);
        

        And of course I have eventFilter in mainWindow:

        bool MainWindow::eventFilter(QObject *object, QEvent *event)
        {
            if( object == textEdit )
            {
                if(event->type() == QEvent::Type::KeyPress) 
                {
                    qInfo()<<"some Text";
                    event->ignore();
                    return true;
                }
                else
                {
                    return ...... // I don't know what I should return here
                }
            }
            else if(object == this)
            {
                if(event->type() == QEvent::Type::KeyPress) 
                {
                    qInfo()<<"some Text";
                    event->ignore();
                    return true;
                }
                else
                {
                    return QMainWindow::eventFilter(object, event);
                }
            }
        }
        

        In (object == this ( mainWindow )) I return "QMainWindow::eventFilter(object, event);" because MainWindow inherits QMainWindow, so I think I should return in (object == textEdit) "QTextEdit::eventFilter(object, event);". But I can't. I get errors:

        1. call to non-static member function without an object argument
        2. eventFilter is protected member of QAbstractScrollArea
        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #3

        @qwe3 use return QMainWindow::eventFilter(object, event);

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          qwe3
          wrote on last edited by
          #4

          @jsulm @eyllanesc Thank you for replies.

          @eyllanesc Why QMainWindow::eventFilter(object, event); ? I don' see link between QMainWindow and myTextEdit class.

          eyllanescE 1 Reply Last reply
          0
          • Q qwe3

            @jsulm @eyllanesc Thank you for replies.

            @eyllanesc Why QMainWindow::eventFilter(object, event); ? I don' see link between QMainWindow and myTextEdit class.

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #5

            @qwe3 If you don't want to modify the behavior then just call the base class method.

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply
            1
            • Q Offline
              Q Offline
              qwe3
              wrote on last edited by
              #6

              @eyllanesc But if I return QMainWindow::eventFilter(object, event);, other events than QEvent::Type::KeyPress will go to event() method of myTextEdit ?

              eyllanescE 1 Reply Last reply
              0
              • Q qwe3

                @eyllanesc But if I return QMainWindow::eventFilter(object, event);, other events than QEvent::Type::KeyPress will go to event() method of myTextEdit ?

                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on last edited by
                #7

                @qwe3 I do not understand you. If you already returned true in the event:

                bool MainWindow::eventFilter(QObject *object, QEvent *event)
                {
                    if(event->type() == QEvent::Type::KeyPress) {
                            qInfo()<<"some Text";
                            event->ignore();
                            return true;
                     }
                     return QMainWindow::eventFilter(object, event);
                }
                

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                1 Reply Last reply
                1
                • Q Offline
                  Q Offline
                  qwe3
                  wrote on last edited by
                  #8

                  @eyllanesc Ok, I understand :)

                  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