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. KeyBoard Event in QListWidget
Forum Updated to NodeBB v4.3 + New Features

KeyBoard Event in QListWidget

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.9k 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.
  • A Offline
    A Offline
    ankush205
    wrote on last edited by
    #1

    I want to use keyboard up down left right buttons .
    So i have re-implemented the QListWidget class
    @class qListWidget :
    public QListWidget
    {
    public:
    qListWidget(void)
    {
    }

    ~qListWidget(void)
    {
    }
    void keyPressEvent(QKeyEvent* event)
    {
    QListWidget::keyPressEvent(event);
    emit itemSelectionChanged();

    }
    }@
    Now What signal should i Emit
    I initially thought of emitting itemSelectionChanged()
    and but what slot should i connect it to
    Suppose i connect to a self defined function MyFunc()

    @ connect(listwidget,SIGNAL(itemSelectionChanged(),this,SLOT(MyFuc())) @

    what code should i put in MyFunc function to get current select item
    @
    QlistWidget* MyFuc()
    {
    ??
    }
    @

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Binary91
      wrote on last edited by
      #2

      First of all, you have to get the ID of the pressed key:
      @
      class my_list : public QListWidget
      {
      Q_OBJECT
      private:
      int key_pressed = 0;

      public:
      my_list(QWidget* parent = 0);

      void keyPressEvent(QKeyEvent* event)
      {
      QListWidget::keyPressEvent(event);
      key_pressed = event->key(); // store Qt::Key value in variable key_pressed
      emit itemSelectionChanged();
      }

      public slots:
      void MyFunc();
      }
      @
      In function MyFunc(), you'll then have to check which key was pressed and react on that, if necessary:
      @my_list *list = new my_list();
      connect(list,SIGNAL(itemSelectionChanged(), list,SLOT(MyFuc()));

      void my_list::MyFunc()
      {
      int row = this->indexFromItem(this->currentItem())->row();
      int column = this->indexFromItem(this->currentItem())->column();

      if(key_pressed == Qt::Key_Left)
      column--;
      else if(key_pressed == Qt::Key_Right)
      column++;
      else if(key_pressed == Qt::Key_Down)
      row--;
      else if(key_pressed == Qt::Key_Up)
      row++;

      if(key_pressed != 0)
      this->setCurrentItem(itemAt(row, column));
      key_pressed = 0;
      }@
      I did not check this code, but maybe that gives you an idea how to handle that...

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

        Hi,

        Why do you need to reimplement keyPressEvent ? Keyboard navigation is already active on QListWidget ?

        currentItem looks like the function you need

        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
        • A Offline
          A Offline
          ankush205
          wrote on last edited by
          #4

          @SGaist But how do we know when an item selection has changed

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

            Connect a slot to the itemSelectionChanged signal, or depending on your needs currentItemChanged. There's no need to reimplement anything keyboard wise, it's already working.

            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