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. eventFilter run many times
Forum Updated to NodeBB v4.3 + New Features

eventFilter run many times

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 986 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    https://github.com/sonichy/HTYFileManager
    I am writing a file manager, and I want to capture enter to open file.
    The lineEditLocation, listView and tableView will emit enter press event.
    Use eventFilter, every object run 3 times !
    When I open a image file in QListView, it will open 3 windows of the image.

    lineEditLocation->installEventFilter(this);
    ui->listView->installEventFilter(this);
    ui->tableView->installEventFilter(this);
    
    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
        QKeyEvent *KE = static_cast<QKeyEvent*>(event);
        if (KE->key() == Qt::Key_Return || KE->key() == Qt::Key_Enter) {
            qDebug() << "eventFilter" << obj;
            if (obj == lineEditLocation) {
                openL();
            } else if (obj == ui->listView) {
                if(ui->listView->selectionModel()->selectedIndexes().size() != 0)
                    open(ui->listView->selectionModel()->selectedIndexes().at(0));            
            } else if (obj == ui->tableView) {
                if(ui->tableView->selectionModel()->selectedIndexes().size() != 0)
                    open(ui->tableView->selectionModel()->selectedIndexes().at(0));            
            }
        }
        return QObject::eventFilter(obj, event);
    }
    

    https://github.com/sonichy

    1 Reply Last reply
    0
    • Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #2

      Hello,

      You have to return true when the event has been "consumed". I think the event is propagated three times as you have installed three event filters and you don't stop event propagation.

      lineEditLocation->installEventFilter(this);
      ui->listView->installEventFilter(this);
      ui->tableView->installEventFilter(this);
      
      bool MainWindow::eventFilter(QObject *obj, QEvent *event)
      {
          QKeyEvent *KE = static_cast<QKeyEvent*>(event);
          if (KE->key() == Qt::Key_Return || KE->key() == Qt::Key_Enter) {
              qDebug() << "eventFilter" << obj;
              if (obj == lineEditLocation) {
                  openL();
              } else if (obj == ui->listView) {
                  if(ui->listView->selectionModel()->selectedIndexes().size() != 0)
                      open(ui->listView->selectionModel()->selectedIndexes().at(0));            
              } else if (obj == ui->tableView) {
                  if(ui->tableView->selectionModel()->selectedIndexes().size() != 0)
                      open(ui->tableView->selectionModel()->selectedIndexes().at(0));            
              } else return QObject::eventFilter(obj, event);
              return true;  //return TRUE, stopping event propagation
          }
          return QObject::eventFilter(obj, event);
      }
      

      This could be the issue.
      From the doc: "The eventFilter() function must return true if the event should be filtered, (i.e. stopped); otherwise it must return false."

      sonichyS 1 Reply Last reply
      5
      • Gojir4G Gojir4

        Hello,

        You have to return true when the event has been "consumed". I think the event is propagated three times as you have installed three event filters and you don't stop event propagation.

        lineEditLocation->installEventFilter(this);
        ui->listView->installEventFilter(this);
        ui->tableView->installEventFilter(this);
        
        bool MainWindow::eventFilter(QObject *obj, QEvent *event)
        {
            QKeyEvent *KE = static_cast<QKeyEvent*>(event);
            if (KE->key() == Qt::Key_Return || KE->key() == Qt::Key_Enter) {
                qDebug() << "eventFilter" << obj;
                if (obj == lineEditLocation) {
                    openL();
                } else if (obj == ui->listView) {
                    if(ui->listView->selectionModel()->selectedIndexes().size() != 0)
                        open(ui->listView->selectionModel()->selectedIndexes().at(0));            
                } else if (obj == ui->tableView) {
                    if(ui->tableView->selectionModel()->selectedIndexes().size() != 0)
                        open(ui->tableView->selectionModel()->selectedIndexes().at(0));            
                } else return QObject::eventFilter(obj, event);
                return true;  //return TRUE, stopping event propagation
            }
            return QObject::eventFilter(obj, event);
        }
        

        This could be the issue.
        From the doc: "The eventFilter() function must return true if the event should be filtered, (i.e. stopped); otherwise it must return false."

        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        @Gojir4 Thank you !

        https://github.com/sonichy

        1 Reply Last reply
        1
        • G Offline
          G Offline
          garycho
          wrote on last edited by garycho
          #4

          Pressing any key within a child widget will return in 3 events

          QEvent::ShortcutOverride
          QEvent::KeyPress
          QEvent::KeyRelease

          We would handle the event of interest only if condition below is met

          if ( keyEvent->type() == QEvent::KeyPress)
          {

          }

          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