title: How to close an editor created by a custom QItemDelegate::createEditor()
body: <p>I have created a custom item delegate which lets users edit a list of file paths:</p>
  
  <p><img src="https://i.stack.imgur.com/OGJtR.png" alt="screenshot"></p>
  
  <p>I have achieved this through a custom class DirEdit. Now the selected path is commited and the editor is closed when the user presses enter, but I would like to add two cases where the editor should be closed without the user having to press enter:</p>
  
  <ol>
  <li>When the user selects a file by activating a combo box entry(by clicking or pressing return)</li>
  <li>When the user selects a file by clicking the "ellipsis" tool button.</li>
  </ol>
  
  <p>I have been exprimenting with clearFocus() and other methods, but nothing seems to work. Below is a complete example:</p>
  
  <pre><code>#include &lt;QtWidgets&gt;
  
  class DirEdit : public QWidget
  {
      QLineEdit* lineEdit=nullptr;
  public:
      DirEdit(QWidget* parent=nullptr)
          : QWidget(parent)
      {
          new QHBoxLayout(this);
          layout()-&gt;setMargin(0);
          layout()-&gt;addWidget(lineEdit=new QLineEdit(this));
  
          QCompleter *completer = new QCompleter(this);
  
          auto model = new QDirModel(completer);
          model-&gt;setFilter(QDir::AllDirs|QDir::NoDotAndDotDot);
          completer-&gt;setModel(model);
  
          lineEdit-&gt;setCompleter(completer);
          connect(completer, static_cast&lt;void (QCompleter::*)(const QString&amp;)&gt;(&amp;QCompleter::activated), [this](const QString&amp; text)
              {
  // &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; TODO: Make the editor close here &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
              });
  
          QToolButton* dotDotDot;
          layout()-&gt;addWidget(dotDotDot=new QToolButton(this));
          dotDotDot-&gt;setText("...");
          connect(dotDotDot, &amp;QToolButton::clicked, this, [this]()
              {
                  QString dir = QFileDialog::getExistingDirectory(window(), "Select Directory", lineEdit-&gt;text());
                  if(dir!="")
                  {
                      lineEdit-&gt;setText(dir);
  // &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; TODO: Make the editor close here &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
                  }
              });
          setFocusProxy(lineEdit);
      }
      void setPath(const QString&amp; path)
      {
          lineEdit-&gt;setText(path);
      }
      QString path()const
      {
          return lineEdit-&gt;text();
      }
  };
  
  class MyDelegate : public QItemDelegate
  {
      QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &amp;, const QModelIndex &amp;index) const
      {
          return new DirEdit(parent);
      }
  
      void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem&amp; option, const QModelIndex &amp;)const
      {
          editor-&gt;setGeometry(option.rect);
      }
  
      void setEditorData(QWidget *editor, const QModelIndex &amp;index) const
      {
          QVariant value = index.model()-&gt;data(index, Qt::DisplayRole);
  
          if (DirEdit *dirEdit = dynamic_cast&lt;DirEdit *&gt;(editor))
              dirEdit-&gt;setPath(value.toString());
      }
  
      void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index) const
      {
          if (DirEdit *dirEdit = dynamic_cast&lt;DirEdit *&gt;(editor))
              model-&gt;setData(index, dirEdit-&gt;path());
      }
  };
  
  int main(int argc, char* argv[])
  {
      QApplication app(argc, argv);
      QListWidget listWidget;
  
      listWidget.setItemDelegate(new MyDelegate);
  
      listWidget.addItem(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
      listWidget.addItem(QStandardPaths::writableLocation(QStandardPaths::MusicLocation));
      listWidget.addItem(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
      listWidget.addItem(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
  
      for (int i = 0; i&lt;listWidget.count(); i++)
          listWidget.item(i)-&gt;setFlags(listWidget.item(0)-&gt;flags()|Qt::ItemIsEditable);
  
      listWidget.show();
      return app.exec();
  }
  </code></pre>