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. User-Defined Action for a QListWidget on Enter Keystroke
Forum Updated to NodeBB v4.3 + New Features

User-Defined Action for a QListWidget on Enter Keystroke

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 3.4k 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.
  • ModelTechM Offline
    ModelTechM Offline
    ModelTech
    wrote on last edited by
    #1

    I have created a subclass of QListWidget called ClassList to list a number of items. These items are selectable and I created a menu for performing an action using ClassList::onShowContextMenu, which connects to a slot that I have called ClassList::onEndit. All this actually works perfectly fine.

    Now, I would like to add the possibility of performing the action implemented via slot ClassList::onEdit when the Enter/Return key is pressed. How can I realize this? I have found some examples with event filters but I don't really understand how they would work for my case where the action is realized by a slot that I defined myself. Any suggestions?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      How about reimplementing key press event for list widget ? Or create another object and install it event filter. If event is return keyevent call your on edit function. I can provide u sample if u want.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      ModelTechM 1 Reply Last reply
      2
      • dheerendraD dheerendra

        How about reimplementing key press event for list widget ? Or create another object and install it event filter. If event is return keyevent call your on edit function. I can provide u sample if u want.

        ModelTechM Offline
        ModelTechM Offline
        ModelTech
        wrote on last edited by
        #3

        @dheerendra

        Reimplementing key press event does not work for the Enter key, so I will need an event filter. I would be very pleased with an example that fits to my context

        raven-worxR 1 Reply Last reply
        0
        • ModelTechM ModelTech

          @dheerendra

          Reimplementing key press event does not work for the Enter key, so I will need an event filter. I would be very pleased with an example that fits to my context

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @ModelTech said in User-Defined Action for a QListWidget on Enter Keystroke:

          Reimplementing key press event does not work for the Enter key

          please show your implementation.
          When exactly do you want the ENTER/RETURN key to be triggered? I mean in which state is the listwidget at this time?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • ModelTechM Offline
            ModelTechM Offline
            ModelTech
            wrote on last edited by
            #5

            Ok. I was not overriding the correct method. I reimplemented keyPressEvent while I should have reimplemented the generic event method. Now I have found a working solution :)

            raven-worxR 1 Reply Last reply
            0
            • ModelTechM ModelTech

              Ok. I was not overriding the correct method. I reimplemented keyPressEvent while I should have reimplemented the generic event method. Now I have found a working solution :)

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @ModelTech
              Actually the base implementation of the generic event handler forwards the events to the specific event handlers (simply for convenience reasons). So the issue was somewhere else.
              But at least its working now ;)

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • ModelTechM Offline
                ModelTechM Offline
                ModelTech
                wrote on last edited by ModelTech
                #7

                Hmm, yes I now also got it to work with keyPressEvent. So, I must have typed something wrongly, I just can't remember what the difference was. Anyway, it now works with keyPressEvent which requires less code than reimplementing event :)

                1 Reply Last reply
                0
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  You can make it work with re-implementing event(..)
                  MyListWidget is inherited from QListWidget

                  bool MyListWidget::event(QEvent *e)
                  {
                  if (e->type() == QEvent::KeyPress){
                  QKeyEvent e1 = static_cast<QKeyEvent>(e);
                  if (e1->key()== Qt::Key_Return){
                  this->callMe();
                  return true;
                  }
                  }
                  return QWidget::event(e);
                  }

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  3

                  • Login

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