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. How to detect if "Enter Key" has been hit when a QlistWidget item is selected.
Forum Updated to NodeBB v4.3 + New Features

How to detect if "Enter Key" has been hit when a QlistWidget item is selected.

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 665 Views 3 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.
  • ademmlerA Offline
    ademmlerA Offline
    ademmler
    wrote on last edited by
    #1

    Hi, here is what I try to implement.

    I have a QWidget (JobBrowser) which has a QLineEdit (Search Phrase) and a QListWidget.
    The user will have a Keyboard only - no mouse!

    • First he types a search phrase into the LineEdit and hit enter.
    • The results are shown in the QListWidget and the first entry is selected.
    • The user may choose another entry (by arrow keys) and hits enter again!
    • Now the files will be processed.

    In my actual implementation the first entry gets automatically executed, when it is listed.
    How can I combine the KeyEvent and the active Widget in order to distinguish which action has to be executed.

    void JobBrowser::keyPressEvent(QKeyEvent* event)
    {
        if(ui->listWidget->count() == 0)
        {
            JobBrowser::keyPressEvent(event);
            return;
        }
    
        if (event->key() == Qt::Key_Enter) {
        QListWidgetItem *item = ui->listWidget->currentItem();
        if (item->isSelected()) {
            QString filePath = jobBrowserFolder.absoluteFilePath(item->text());
            emit signalSendFilePath(filePath);
            }
        }
    }
    
    1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      If I understood you right, you'd like the arrow key press event to adjust selection on the list widget?

      In that case add another if.

      if (event->key() == Qt::Key_Up) { //or Qt::Key_Down
      //move selection in the listWidget up or down, you can do that programatically
      }
      

      On the side note: depending how many entries you have in the list widget you might run into performance problems with widget based approach. I found out that having a few hundred items is much smoother with the model/view approach. A bit more work for you but essentially same approach.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      0
      • ademmlerA Offline
        ademmlerA Offline
        ademmler
        wrote on last edited by ademmler
        #3

        @artwaw

        Dear Artur,

        Yes and No. I do not want to reorder the entries. I want to choose one and than hit enter to perform some action with this selection. But Because the LineEdit als reacts on Qt::Key_Enter I need to distinguish if we are within QLineEdit or QListWidget and process Qt::Key_Enter different than.

        artwawA 1 Reply Last reply
        0
        • ademmlerA ademmler

          @artwaw

          Dear Artur,

          Yes and No. I do not want to reorder the entries. I want to choose one and than hit enter to perform some action with this selection. But Because the LineEdit als reacts on Qt::Key_Enter I need to distinguish if we are within QLineEdit or QListWidget and process Qt::Key_Enter different than.

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @ademmler
          Ah, I don't think we're on the same page!
          I did not imply reordering. Just manipulating a selection.
          The way I dealt with it in the past was by using a model and a list view with filtering, so please bear with me.

          Since you mentioned the need to distinguish between which widget is currently active, you can check that by using hasFocus() method?

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • ademmlerA Offline
            ademmlerA Offline
            ademmler
            wrote on last edited by
            #5

            @artwaw

            I lgave it a try but it did not helped (so far). The reason because the ListWidget has always the focus at this stage. Hence processing starts regardless if Enter had been hit or not.

            Pl45m4P 1 Reply Last reply
            0
            • ademmlerA ademmler

              @artwaw

              I lgave it a try but it did not helped (so far). The reason because the ListWidget has always the focus at this stage. Hence processing starts regardless if Enter had been hit or not.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @ademmler

              I've seen something similar in other programs before. There was the Space key used to (un-)select items and then Enter key to continue. So maybe don't use the same key for both actions.

              Btw:

              @ademmler said in How to detect if "Enter Key" has been hit when a QlistWidget item is selected.:

              void JobBrowser::keyPressEvent(QKeyEvent* event)
              {
                  if(ui->listWidget->count() == 0)
                  {
                      JobBrowser::keyPressEvent(event);
                      return;
                  }
              }
              

              Isn't this a recursive deadlock when count() == 0?!
              You handle the event, check if count = 0, then pass the event to JobBrowser::keyPressEvent handler again...
              Either pass it to JobBrowsers base class' event handler and/or call event->accept() / event->ignore(), so that this event (the key action) is flagged as successfully handled.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              1
              • ademmlerA Offline
                ademmlerA Offline
                ademmler
                wrote on last edited by ademmler
                #7

                @Pl45m4

                thx for the input. Yes you are right. In the meantime I changed my code to this.
                However, I am not sure where I have to out event->accept() or event->ignore() calls correctly.
                May you help me out with this.

                void JobBrowser::keyPressEvent(QKeyEvent* event)
                {
                   
                    if (event->key() != Qt::Key_Enter)
                    {
                        JobBrowser::keyPressEvent(event);
                        return;
                    }
                
                    if(ui->listWidget->count() != 0) {
                    QListWidgetItem *item = ui->listWidget->currentItem();
                    if (item->isSelected()) {
                        QString filePath = jobBrowserFolder.absoluteFilePath(item->text());
                        emit signalSendFilePath(filePath);
                        }
                
                    }
                }
                
                1 Reply Last reply
                0
                • ademmlerA Offline
                  ademmlerA Offline
                  ademmler
                  wrote on last edited by
                  #8

                  Hi Agin, can somebody please tell me what is best practise with Overwriting Key Events and placing event->accept() / event->ignore() ?

                  JonBJ 1 Reply Last reply
                  0
                  • ademmlerA ademmler

                    Hi Agin, can somebody please tell me what is best practise with Overwriting Key Events and placing event->accept() / event->ignore() ?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @ademmler
                    While you await a better answer.

                    I think you should read https://doc.qt.io/qt-6/qkeyevent.html if you haven't already:

                    A key event contains a special accept flag that indicates whether the receiver will handle the key event. This flag is set by default for QEvent::KeyPress and QEvent::KeyRelease, so there is no need to call accept() when acting on a key event. For QEvent::ShortcutOverride the receiver needs to explicitly accept the event to trigger the override. Calling ignore() on a key event will propagate it to the parent widget. The event is propagated up the parent widget chain until a widget accepts it or an event filter consumes it.

                    And you might also take time to read through https://www.learnqt.guide/working-with-events (e.g. search it for ignore, there's quite a bit of discussion).

                    So, the event should already come in "accepted" (check by printing that out). It therefore does not matter whether you accept() it or not, though for clarity I probably would in your case where you handle Key_Enter. ignore() would request handling of the key outside of your JobBrowser, which I don't think you want, I'm not sure of an example where you would do so. Maybe have a look at https://stackoverflow.com/questions/21936037/how-to-accept-ignore-qkeyevent.

                    1 Reply Last reply
                    0
                    • ademmlerA Offline
                      ademmlerA Offline
                      ademmler
                      wrote on last edited by
                      #10

                      @JonB

                      Hi JonB Thx for pointing this out! I ll dive deeper an try do understand.

                      1 Reply Last reply
                      0
                      • ademmlerA Offline
                        ademmlerA Offline
                        ademmler
                        wrote on last edited by ademmler
                        #11

                        Ok Gentlemen,

                        I have tested some things and ran into a couple of issues (Crashes of the application).

                        The only approach wich works a expected and without crashing is this:

                        void JobBrowser::keyPressEvent(QKeyEvent* event)
                        {
                        
                            // if (event->key() != Qt::Key_Enter)
                            // {
                            //     JobBrowser::keyPressEvent(event);
                            //     return;
                            // }
                        
                            if (ui->listWidget_Jobs->count() == 0 && event->key() == Qt::Key_Enter) {
                                getMatchingFiles(ui->lineEdit_searchPhrase->text());
                            }
                            else if(ui->listWidget_Jobs->count() != 0 && event->key() == Qt::Key_Enter) {
                                QListWidgetItem *item = ui->listWidget_Jobs->currentItem();
                                if (item->isSelected()) {
                                    QString filePath = jobBrowserFolder.absoluteFilePath(item->text());
                                    emit signalSendFilePath(filePath);
                                }
                            } else if (event->key() == Qt::Key_F17) {
                                ui->checkBox_Front->toggle();
                            } else if(event->key() == Qt::Key_F18) {
                                ui->checkBox_Back->toggle();
                            } else {
                                event->accept();
                            }
                        }
                        

                        But now I am confused, because it only works if I deactivate the part where I pass the standard event.
                        What is my logical or technical problem here.

                        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