diff --git a/src/libs/utils/pathchooser.cpp b/src/libs/utils/pathchooser.cpp index 7fde56993e4..142d061682a 100644 --- a/src/libs/utils/pathchooser.cpp +++ b/src/libs/utils/pathchooser.cpp @@ -26,6 +26,7 @@ #include "pathchooser.h" #include "environment.h" +#include "optional.h" #include "qtcassert.h" #include "synchronousprocess.h" @@ -340,14 +341,22 @@ QString PathChooser::expandedDirectory(const QString &input, const Environment & return path; } +void setTextKeepingActiveCursor(QLineEdit *edit, const QString &text) +{ + optional cursor = edit->hasFocus() ? make_optional(edit->cursorPosition()) : nullopt; + edit->setText(text); + if (cursor) + edit->setCursorPosition(*cursor); +} + void PathChooser::setPath(const QString &path) { - d->m_lineEdit->setText(QDir::toNativeSeparators(path)); + setTextKeepingActiveCursor(d->m_lineEdit, QDir::toNativeSeparators(path)); } void PathChooser::setFileName(const FileName &fn) { - d->m_lineEdit->setText(fn.toUserOutput()); + setTextKeepingActiveCursor(d->m_lineEdit, fn.toUserOutput()); } void PathChooser::setErrorColor(const QColor &errorColor)