QAbstractItemView Tab When Editing an Element

I have QTreeViewfilled with elements from the model. When a call to is edit()made by index, a custom editor is displayed. The editor consists of two QLineEditwidgets.

enter image description here

I want the focus to switch between two QLineEditwidgets when you press Tab. However, pressing Tab cycles through everything else in my program. All my items QPushButtonand QTabWidgetare included in the order of the tabs, even if they are completely different widgets than my editor.

I tried to set the tab order with help setTabOrder()to encode it between two widgets QLineEdit, however this still does not isolate the editor widget from the surrounding widgets. Why is this happening?

NOTE. I am not trying to disable the tab setting elsewhere, just isolate it to my editor.

Thank you for your time!

+4
source share
1 answer

This can be easily implemented using QWidget::focusNextPrevChildas 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;
};
+2
source

Source: https://habr.com/ru/post/1665393/


All Articles