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. Make QTableView behave identical for Key_Up and Key_Tab when there are delegates

Make QTableView behave identical for Key_Up and Key_Tab when there are delegates

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 478 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.
  • O Offline
    O Offline
    olowo726
    wrote on last edited by
    #1

    I have a QTable in which one column sometimes, i.e. for some rows, have a delegate editor of type QLineEdit. User can only select entire row. When one QLineEdit have focus and the user presses Key_Up/Key_Down the previous/next row is selected and previous/next QLineEdit automatically gets focus. How can I get the same behaviour for tab? I've tried to capture the key events in the table view, i.e.

    void ScalarTableTableView::keyPressEvent(QKeyEvent *event)
    {
        // Change tab to arrow up/down to be able to move to next row with one key.
        if(event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab)
        {
            QKeyEvent newEvent(event->type(),
                               event->key() == Qt::Key_Tab ? Qt::Key_Down : Qt::Key_Up,
                               event->modifiers(),
                               event->nativeScanCode(),
                               event->nativeModifiers(),
                               event->nativeVirtualKey(),
                               event->text(),
                               event->isAutoRepeat(),
                               event->count());
            event->accept();
            MoveBetweenViewsWithDragAndDropTableView::keyPressEvent(&newEvent);
        }
        else
        {
            MoveBetweenViewsWithDragAndDropTableView::keyPressEvent(event);
        }
    }
    

    This works for selecting the previous/next row but the QLineEdit doesn't get focus. Do I need to set focus manually? If so, how can I get the delegate QLineEdit for a certain row?

    eyllanescE 1 Reply Last reply
    0
    • O olowo726

      I have a QTable in which one column sometimes, i.e. for some rows, have a delegate editor of type QLineEdit. User can only select entire row. When one QLineEdit have focus and the user presses Key_Up/Key_Down the previous/next row is selected and previous/next QLineEdit automatically gets focus. How can I get the same behaviour for tab? I've tried to capture the key events in the table view, i.e.

      void ScalarTableTableView::keyPressEvent(QKeyEvent *event)
      {
          // Change tab to arrow up/down to be able to move to next row with one key.
          if(event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab)
          {
              QKeyEvent newEvent(event->type(),
                                 event->key() == Qt::Key_Tab ? Qt::Key_Down : Qt::Key_Up,
                                 event->modifiers(),
                                 event->nativeScanCode(),
                                 event->nativeModifiers(),
                                 event->nativeVirtualKey(),
                                 event->text(),
                                 event->isAutoRepeat(),
                                 event->count());
              event->accept();
              MoveBetweenViewsWithDragAndDropTableView::keyPressEvent(&newEvent);
          }
          else
          {
              MoveBetweenViewsWithDragAndDropTableView::keyPressEvent(event);
          }
      }
      

      This works for selecting the previous/next row but the QLineEdit doesn't get focus. Do I need to set focus manually? If so, how can I get the delegate QLineEdit for a certain row?

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

      @olowo726 You can do the move directly:

      void ScalarTableTableView::keyPressEvent(QKeyEvent *event)
      {
          if(event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab)
          {
              QModelIndex newCurrent = moveCursor(event->key() == Qt::Key_Tab ? MoveDown: MoveUp, event->modifiers());
              QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
              selectionModel()->setCurrentIndex(newCurrent, command);
              return;
          }
          QTableView::keyPressEvent(event);
      }

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

      O 1 Reply Last reply
      0
      • eyllanescE eyllanesc

        @olowo726 You can do the move directly:

        void ScalarTableTableView::keyPressEvent(QKeyEvent *event)
        {
            if(event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab)
            {
                QModelIndex newCurrent = moveCursor(event->key() == Qt::Key_Tab ? MoveDown: MoveUp, event->modifiers());
                QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
                selectionModel()->setCurrentIndex(newCurrent, command);
                return;
            }
            QTableView::keyPressEvent(event);
        }
        O Offline
        O Offline
        olowo726
        wrote on last edited by
        #3

        @eyllanesc Doesn't work, the delegates do not get focus.

        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