mrg95
mrg95

Reputation: 2418

QAbstractItemView Tab Focus While Editing Item

I have a QTreeView populated with items from a model. When a call to edit() is made on an index, a custom editor displays. The editor consists of two QLineEdit widgets.

enter image description here

I want the focus to switch between the two QLineEdit widgets when Tab is pressed. However, pressing Tab cycles through everything else on my program. All my QPushButton and QTabWidget objects are included in the Tab order even though they are completely different widgets than my editor.

I've tried setting the tab order using setTabOrder() to loop it between the two QLineEdit widgets, however this still doesn't isolate the editor widget from the surrounding widgets. Why is this happening?

NOTE: I'm not trying to disable tab ordering anywhere else, just isolate it to my editor for the time being.

Thanks for your time!

Upvotes: 4

Views: 342

Answers (1)

m7913d
m7913d

Reputation: 11072

This can be easily implemented using QWidget::focusNextPrevChild as follows:

class EditWidget : public QWidget
{
public:
  EditWidget(QWidget *pParent) : QWidget(pParent)
  {
    QHBoxLayout *pLayout = new QHBoxLayout(this);
    setLayout(pLayout);
    pLayout->addWidget(m_pEdit1 = new QLineEdit ());
    pLayout->addWidget(m_pEdit2 = new QLineEdit ());
  }

  bool focusNextPrevChild(bool next)
  {
    if (m_pEdit2->hasFocus())
      m_pEdit1->setFocus();
    else
      m_pEdit2->setFocus();
    return true; // prevent further actions (i.e. consume the (tab) event)
  }

protected:
  QLineEdit *m_pEdit1;
  QLineEdit *m_pEdit2;
};

Upvotes: 3

Related Questions