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. Loop in taborder in a QTableView
Forum Updated to NodeBB v4.3 + New Features

Loop in taborder in a QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 2.1k 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.
  • B Offline
    B Offline
    Ben35
    wrote on last edited by
    #1

    Hello all,

    I have a problem with taborder in a QTableView. Indeed, when i browse between my widgets with TAB, if a widget is a QTableView, the focus enters in it and loop over the table cells. The focus is trapped inside the QTableView. TAB just focus the first cell after the last cell.

    I would like the focus to escape the QTableView after tabbing on the last cell.

    An idea ?

    Thanks !
    Ben

    raven-worxR 1 Reply Last reply
    0
    • B Ben35

      Hello all,

      I have a problem with taborder in a QTableView. Indeed, when i browse between my widgets with TAB, if a widget is a QTableView, the focus enters in it and loop over the table cells. The focus is trapped inside the QTableView. TAB just focus the first cell after the last cell.

      I would like the focus to escape the QTableView after tabbing on the last cell.

      An idea ?

      Thanks !
      Ben

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

      @Ben35
      you need to filter the Qt::Key_Tab (and Backtab) key events to your table view.
      If the current index is currently positioned at the end you need to call focusNextChild() / focusPreviousChild()

      Alternatively you can sublass your table view and reimplement moveCursor():

      class MyTable : public QTableView
      {
      virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
      {
           QModelIndex index = QTableView::moveCursor(cursorAction, modifiers);
           switch( cursorAction )
           {
                  case MoveNext:   //TAB
                  case MovePrevious:  //BACKTAB
                  {
                            if( doNotFocusNextItem )  // <<<<<<<<<<<
                            {
                                    index = QModelIndex();   // do not update the current index (see QAbstractItemView::keyPressEvent() handler)
                                    this->focusNextPrevChild( cursorAction == MoveNext );   //let Qt focus next widget
                            }
                  }
                  break;
           }
           return index;
      }
      

      Haven't tested it though.

      --- 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
      1
      • B Offline
        B Offline
        Ben35
        wrote on last edited by
        #3

        Thanks for your reply,

        Before your answer, i tried this (i override the focusNextPrevChild method of my tableview) :

        bool focusNextPrevChild(bool next) override final
        {
        auto index = currentIndex();
        if (index.row() == model()->rowCount() - 1 && index.column() == model()->columnCount() - 1) {
        return QWidget::focusNextPrevChild(next);
        }
        return QTableView::focusNextPrevChild(next);
        }

        It seems to have the behaviour i expected. Do you think it's OK ?

        raven-worxR 1 Reply Last reply
        0
        • B Ben35

          Thanks for your reply,

          Before your answer, i tried this (i override the focusNextPrevChild method of my tableview) :

          bool focusNextPrevChild(bool next) override final
          {
          auto index = currentIndex();
          if (index.row() == model()->rowCount() - 1 && index.column() == model()->columnCount() - 1) {
          return QWidget::focusNextPrevChild(next);
          }
          return QTableView::focusNextPrevChild(next);
          }

          It seems to have the behaviour i expected. Do you think it's OK ?

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

          @Ben35
          oh... i haven't seen that the actual keyPress event is generated in QAbstractItemView::focusNextPrevChild() and sent to the table view widget which then calls moveCursor(),

          So my proposed solution (using moveCursor()) wouldn't work anyway (as it is).

          Yours should be fine.

          --- 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
          • B Offline
            B Offline
            Ben35
            wrote on last edited by Ben35
            #5

            OK. It seems that there is a problem too with my solution. It works when i tab after the last cell in a "focus/selection mode", but if i am in a "cell edit mode" (in which a tab open the editor of the next cell), then the loop is still there ...

            raven-worxR 1 Reply Last reply
            0
            • B Ben35

              OK. It seems that there is a problem too with my solution. It works when i tab after the last cell in a "focus/selection mode", but if i am in a "cell edit mode" (in which a tab open the editor of the next cell), then the loop is still there ...

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

              @Ben35
              in this case you also need to integrate my moveCursor() fix.
              Maybe you should anyway fix it this way (with your tweak to call QWidget's focusNextPrevChild() instead)

              Watch out for endless recursions!

              --- 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

              • Login

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