QLineEdit: Use pmf-style connects

Port all string-based signal/slots connections to pmf-style connects.

Change-Id: I975232a3fedf82cd3327638a0ee119d1f2a90d84
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit bbdc8afa116155a9e8353f1d39af19a2228e3412)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2023-12-23 21:06:01 +01:00 committed by Qt Cherry-pick Bot
parent 47cdc81e94
commit 4fec40e67e
5 changed files with 99 additions and 87 deletions

View File

@ -622,7 +622,7 @@ void QLineEdit::setCompleter(QCompleter *c)
if (c == d->control->completer()) if (c == d->control->completer())
return; return;
if (d->control->completer()) { if (d->control->completer()) {
disconnect(d->control->completer(), nullptr, this, nullptr); d->disconnectCompleter();
d->control->completer()->setWidget(nullptr); d->control->completer()->setWidget(nullptr);
if (d->control->completer()->parent() == this) if (d->control->completer()->parent() == this)
delete d->control->completer(); delete d->control->completer();
@ -632,12 +632,8 @@ void QLineEdit::setCompleter(QCompleter *c)
return; return;
if (c->widget() == nullptr) if (c->widget() == nullptr)
c->setWidget(this); c->setWidget(this);
if (hasFocus()) { if (hasFocus())
QObject::connect(d->control->completer(), SIGNAL(activated(QString)), d->connectCompleter();
this, SLOT(setText(QString)));
QObject::connect(d->control->completer(), SIGNAL(highlighted(QString)),
this, SLOT(_q_completionHighlighted(QString)));
}
} }
/*! /*!
@ -1447,7 +1443,10 @@ bool QLineEdit::event(QEvent * e)
#endif #endif
//d->separate(); //d->separate();
} else if (e->type() == QEvent::WindowActivate) { } else if (e->type() == QEvent::WindowActivate) {
QTimer::singleShot(0, this, SLOT(_q_handleWindowActivate())); QTimer::singleShot(0, this, [this]() {
Q_D(QLineEdit);
d->handleWindowActivate();
});
#ifndef QT_NO_SHORTCUT #ifndef QT_NO_SHORTCUT
} else if (e->type() == QEvent::ShortcutOverride) { } else if (e->type() == QEvent::ShortcutOverride) {
QKeyEvent *ke = static_cast<QKeyEvent*>(e); QKeyEvent *ke = static_cast<QKeyEvent*>(e);
@ -1924,10 +1923,7 @@ void QLineEdit::focusInEvent(QFocusEvent *e)
#if QT_CONFIG(completer) #if QT_CONFIG(completer)
if (d->control->completer()) { if (d->control->completer()) {
d->control->completer()->setWidget(this); d->control->completer()->setWidget(this);
QObject::connect(d->control->completer(), SIGNAL(activated(QString)), d->connectCompleter();
this, SLOT(setText(QString)));
QObject::connect(d->control->completer(), SIGNAL(highlighted(QString)),
this, SLOT(_q_completionHighlighted(QString)));
} }
#endif #endif
update(); update();
@ -1966,9 +1962,8 @@ void QLineEdit::focusOutEvent(QFocusEvent *e)
d->control->setCancelText(QString()); d->control->setCancelText(QString());
#endif #endif
#if QT_CONFIG(completer) #if QT_CONFIG(completer)
if (d->control->completer()) { if (d->control->completer())
QObject::disconnect(d->control->completer(), nullptr, this, nullptr); d->disconnectCompleter();
}
#endif #endif
QWidget::focusOutEvent(e); QWidget::focusOutEvent(e);
} }
@ -2215,12 +2210,12 @@ QMenu *QLineEdit::createStandardContextMenu()
action = popup->addAction(QLineEdit::tr("&Undo") + ACCEL_KEY(QKeySequence::Undo)); action = popup->addAction(QLineEdit::tr("&Undo") + ACCEL_KEY(QKeySequence::Undo));
action->setEnabled(d->control->isUndoAvailable()); action->setEnabled(d->control->isUndoAvailable());
setActionIcon(action, QStringLiteral("edit-undo")); setActionIcon(action, QStringLiteral("edit-undo"));
connect(action, SIGNAL(triggered()), SLOT(undo())); connect(action, &QAction::triggered, this, &QLineEdit::undo);
action = popup->addAction(QLineEdit::tr("&Redo") + ACCEL_KEY(QKeySequence::Redo)); action = popup->addAction(QLineEdit::tr("&Redo") + ACCEL_KEY(QKeySequence::Redo));
action->setEnabled(d->control->isRedoAvailable()); action->setEnabled(d->control->isRedoAvailable());
setActionIcon(action, QStringLiteral("edit-redo")); setActionIcon(action, QStringLiteral("edit-redo"));
connect(action, SIGNAL(triggered()), SLOT(redo())); connect(action, &QAction::triggered, this, &QLineEdit::redo);
popup->addSeparator(); popup->addSeparator();
} }
@ -2231,20 +2226,20 @@ QMenu *QLineEdit::createStandardContextMenu()
action->setEnabled(!d->control->isReadOnly() && d->control->hasSelectedText() action->setEnabled(!d->control->isReadOnly() && d->control->hasSelectedText()
&& d->control->echoMode() == QLineEdit::Normal); && d->control->echoMode() == QLineEdit::Normal);
setActionIcon(action, QStringLiteral("edit-cut")); setActionIcon(action, QStringLiteral("edit-cut"));
connect(action, SIGNAL(triggered()), SLOT(cut())); connect(action, &QAction::triggered, this, &QLineEdit::cut);
} }
action = popup->addAction(QLineEdit::tr("&Copy") + ACCEL_KEY(QKeySequence::Copy)); action = popup->addAction(QLineEdit::tr("&Copy") + ACCEL_KEY(QKeySequence::Copy));
action->setEnabled(d->control->hasSelectedText() action->setEnabled(d->control->hasSelectedText()
&& d->control->echoMode() == QLineEdit::Normal); && d->control->echoMode() == QLineEdit::Normal);
setActionIcon(action, QStringLiteral("edit-copy")); setActionIcon(action, QStringLiteral("edit-copy"));
connect(action, SIGNAL(triggered()), SLOT(copy())); connect(action, &QAction::triggered, this, &QLineEdit::copy);
if (!isReadOnly()) { if (!isReadOnly()) {
action = popup->addAction(QLineEdit::tr("&Paste") + ACCEL_KEY(QKeySequence::Paste)); action = popup->addAction(QLineEdit::tr("&Paste") + ACCEL_KEY(QKeySequence::Paste));
action->setEnabled(!d->control->isReadOnly() && !QGuiApplication::clipboard()->text().isEmpty()); action->setEnabled(!d->control->isReadOnly() && !QGuiApplication::clipboard()->text().isEmpty());
setActionIcon(action, QStringLiteral("edit-paste")); setActionIcon(action, QStringLiteral("edit-paste"));
connect(action, SIGNAL(triggered()), SLOT(paste())); connect(action, &QAction::triggered, this, &QLineEdit::paste);
} }
#endif #endif
@ -2252,7 +2247,8 @@ QMenu *QLineEdit::createStandardContextMenu()
action = popup->addAction(QLineEdit::tr("Delete")); action = popup->addAction(QLineEdit::tr("Delete"));
action->setEnabled(!d->control->isReadOnly() && !d->control->text().isEmpty() && d->control->hasSelectedText()); action->setEnabled(!d->control->isReadOnly() && !d->control->text().isEmpty() && d->control->hasSelectedText());
setActionIcon(action, QStringLiteral("edit-delete")); setActionIcon(action, QStringLiteral("edit-delete"));
connect(action, SIGNAL(triggered()), d->control, SLOT(_q_deleteSelected())); connect(action, &QAction::triggered,
d->control, &QWidgetLineControl::_q_deleteSelected);
} }
if (!popup->isEmpty()) if (!popup->isEmpty())
@ -2262,7 +2258,7 @@ QMenu *QLineEdit::createStandardContextMenu()
action->setEnabled(!d->control->text().isEmpty() && !d->control->allSelected()); action->setEnabled(!d->control->text().isEmpty() && !d->control->allSelected());
setActionIcon(action, QStringLiteral("edit-select-all")); setActionIcon(action, QStringLiteral("edit-select-all"));
d->selectAllAction = action; d->selectAllAction = action;
connect(action, SIGNAL(triggered()), SLOT(selectAll())); connect(action, &QAction::triggered, this, &QLineEdit::selectAll);
if (!d->control->isReadOnly() && QGuiApplication::styleHints()->useRtlExtensions()) { if (!d->control->isReadOnly() && QGuiApplication::styleHints()->useRtlExtensions()) {
popup->addSeparator(); popup->addSeparator();

View File

@ -214,20 +214,6 @@ private:
#endif #endif
Q_DISABLE_COPY(QLineEdit) Q_DISABLE_COPY(QLineEdit)
Q_DECLARE_PRIVATE(QLineEdit) Q_DECLARE_PRIVATE(QLineEdit)
Q_PRIVATE_SLOT(d_func(), void _q_handleWindowActivate())
Q_PRIVATE_SLOT(d_func(), void _q_textEdited(const QString &))
Q_PRIVATE_SLOT(d_func(), void _q_cursorPositionChanged(int, int))
#if QT_CONFIG(completer)
Q_PRIVATE_SLOT(d_func(), void _q_completionHighlighted(const QString &))
#endif
#ifdef QT_KEYPAD_NAVIGATION
Q_PRIVATE_SLOT(d_func(), void _q_editFocusChange(bool))
#endif
Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged())
Q_PRIVATE_SLOT(d_func(), void _q_updateNeeded(const QRect &))
Q_PRIVATE_SLOT(d_func(), void _q_textChanged(const QString &))
Q_PRIVATE_SLOT(d_func(), void _q_clearButtonClicked())
Q_PRIVATE_SLOT(d_func(), void _q_controlEditingFinished())
}; };
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -74,8 +74,25 @@ QRect QLineEditPrivate::cursorRect() const
} }
#if QT_CONFIG(completer) #if QT_CONFIG(completer)
void QLineEditPrivate::connectCompleter()
{
Q_Q(const QLineEdit);
QObject::connect(control->completer(), qOverload<const QString &>(&QCompleter::activated),
q, &QLineEdit::setText);
QObjectPrivate::connect(control->completer(), qOverload<const QString &>(&QCompleter::highlighted),
this, &QLineEditPrivate::completionHighlighted);
}
void QLineEditPrivate::_q_completionHighlighted(const QString &newText) void QLineEditPrivate::disconnectCompleter()
{
Q_Q(const QLineEdit);
QObject::disconnect(control->completer(), qOverload<const QString &>(&QCompleter::activated),
q, &QLineEdit::setText);
QObjectPrivate::disconnect(control->completer(), qOverload<const QString &>(&QCompleter::highlighted),
this, &QLineEditPrivate::completionHighlighted);
}
void QLineEditPrivate::completionHighlighted(const QString &newText)
{ {
Q_Q(QLineEdit); Q_Q(QLineEdit);
if (control->completer()->completionMode() != QCompleter::InlineCompletion) { if (control->completer()->completionMode() != QCompleter::InlineCompletion) {
@ -96,14 +113,14 @@ void QLineEditPrivate::_q_completionHighlighted(const QString &newText)
#endif // QT_CONFIG(completer) #endif // QT_CONFIG(completer)
void QLineEditPrivate::_q_handleWindowActivate() void QLineEditPrivate::handleWindowActivate()
{ {
Q_Q(QLineEdit); Q_Q(QLineEdit);
if (!q->hasFocus() && control->hasSelectedText()) if (!q->hasFocus() && control->hasSelectedText())
control->deselect(); control->deselect();
} }
void QLineEditPrivate::_q_textEdited(const QString &text) void QLineEditPrivate::textEdited(const QString &text)
{ {
Q_Q(QLineEdit); Q_Q(QLineEdit);
edited = true; edited = true;
@ -115,7 +132,7 @@ void QLineEditPrivate::_q_textEdited(const QString &text)
#endif #endif
} }
void QLineEditPrivate::_q_cursorPositionChanged(int from, int to) void QLineEditPrivate::cursorPositionChanged(int from, int to)
{ {
Q_Q(QLineEdit); Q_Q(QLineEdit);
q->update(); q->update();
@ -123,14 +140,14 @@ void QLineEditPrivate::_q_cursorPositionChanged(int from, int to)
} }
#ifdef QT_KEYPAD_NAVIGATION #ifdef QT_KEYPAD_NAVIGATION
void QLineEditPrivate::_q_editFocusChange(bool e) void QLineEditPrivate::editFocusChange(bool e)
{ {
Q_Q(QLineEdit); Q_Q(QLineEdit);
q->setEditFocus(e); q->setEditFocus(e);
} }
#endif #endif
void QLineEditPrivate::_q_selectionChanged() void QLineEditPrivate::selectionChanged()
{ {
Q_Q(QLineEdit); Q_Q(QLineEdit);
if (control->preeditAreaText().isEmpty()) { if (control->preeditAreaText().isEmpty()) {
@ -150,7 +167,7 @@ void QLineEditPrivate::_q_selectionChanged()
#endif #endif
} }
void QLineEditPrivate::_q_updateNeeded(const QRect &rect) void QLineEditPrivate::updateNeeded(const QRect &rect)
{ {
q_func()->update(adjustedControlRect(rect)); q_func()->update(adjustedControlRect(rect));
} }
@ -158,45 +175,51 @@ void QLineEditPrivate::_q_updateNeeded(const QRect &rect)
void QLineEditPrivate::init(const QString& txt) void QLineEditPrivate::init(const QString& txt)
{ {
Q_Q(QLineEdit); Q_Q(QLineEdit);
const auto qUpdateMicroFocus = [q]()
{
q->updateMicroFocus();
};
control = new QWidgetLineControl(txt); control = new QWidgetLineControl(txt);
control->setParent(q); control->setParent(q);
control->setFont(q->font()); control->setFont(q->font());
QObject::connect(control, SIGNAL(textChanged(QString)), QObject::connect(control, &QWidgetLineControl::textChanged,
q, SIGNAL(textChanged(QString))); q, &QLineEdit::textChanged);
QObject::connect(control, SIGNAL(textEdited(QString)), QObjectPrivate::connect(control, &QWidgetLineControl::textEdited,
q, SLOT(_q_textEdited(QString))); this, &QLineEditPrivate::textEdited);
QObject::connect(control, SIGNAL(cursorPositionChanged(int,int)), QObjectPrivate::connect(control, &QWidgetLineControl::cursorPositionChanged,
q, SLOT(_q_cursorPositionChanged(int,int))); this, &QLineEditPrivate::cursorPositionChanged);
QObject::connect(control, SIGNAL(selectionChanged()), QObjectPrivate::connect(control, &QWidgetLineControl::selectionChanged,
q, SLOT(_q_selectionChanged())); this, &QLineEditPrivate::selectionChanged);
QObject::connect(control, SIGNAL(editingFinished()), QObjectPrivate::connect(control, &QWidgetLineControl::editingFinished,
q, SLOT(_q_controlEditingFinished())); this, &QLineEditPrivate::controlEditingFinished);
#ifdef QT_KEYPAD_NAVIGATION #ifdef QT_KEYPAD_NAVIGATION
QObject::connect(control, SIGNAL(editFocusChange(bool)), QObject::connect(control, &QWidgetLineControl::editFocusChange,
q, SLOT(_q_editFocusChange(bool))); this, &QLineEditPrivate::editFocusChange);
#endif #endif
QObject::connect(control, SIGNAL(cursorPositionChanged(int,int)), QObject::connect(control, &QWidgetLineControl::cursorPositionChanged,
q, SLOT(updateMicroFocus())); q, qUpdateMicroFocus);
QObject::connect(control, SIGNAL(textChanged(QString)), QObject::connect(control, &QWidgetLineControl::textChanged,
q, SLOT(updateMicroFocus())); q, qUpdateMicroFocus);
QObject::connect(control, SIGNAL(updateMicroFocus()), QObject::connect(control, &QWidgetLineControl::updateMicroFocus,
q, SLOT(updateMicroFocus())); q, qUpdateMicroFocus);
// for now, going completely overboard with updates. // for now, going completely overboard with updates.
QObject::connect(control, SIGNAL(selectionChanged()), QObject::connect(control, &QWidgetLineControl::selectionChanged,
q, SLOT(update())); q, qOverload<>(&QLineEdit::update));
QObject::connect(control, SIGNAL(selectionChanged()), QObject::connect(control, &QWidgetLineControl::selectionChanged,
q, SLOT(updateMicroFocus())); q, qUpdateMicroFocus);
QObject::connect(control, SIGNAL(displayTextChanged(QString)), QObject::connect(control, &QWidgetLineControl::displayTextChanged,
q, SLOT(update())); q, qOverload<>(&QLineEdit::update));
QObject::connect(control, SIGNAL(updateNeeded(QRect)), QObjectPrivate::connect(control, &QWidgetLineControl::updateNeeded,
q, SLOT(_q_updateNeeded(QRect))); this, &QLineEditPrivate::updateNeeded);
QObject::connect(control, SIGNAL(inputRejected()), q, SIGNAL(inputRejected())); QObject::connect(control, &QWidgetLineControl::inputRejected,
q, &QLineEdit::inputRejected);
QStyleOptionFrame opt; QStyleOptionFrame opt;
q->initStyleOption(&opt); q->initStyleOption(&opt);
@ -436,7 +459,7 @@ static void displayWidgets(const QLineEditPrivate::SideWidgetEntryList &widgets,
} }
#endif #endif
void QLineEditPrivate::_q_textChanged(const QString &text) void QLineEditPrivate::textChanged(const QString &text)
{ {
if (hasSideWidgets()) { if (hasSideWidgets()) {
const int newTextSize = text.size(); const int newTextSize = text.size();
@ -451,16 +474,16 @@ void QLineEditPrivate::_q_textChanged(const QString &text)
} }
} }
void QLineEditPrivate::_q_clearButtonClicked() void QLineEditPrivate::clearButtonClicked()
{ {
Q_Q(QLineEdit); Q_Q(QLineEdit);
if (!q->text().isEmpty()) { if (!q->text().isEmpty()) {
q->clear(); q->clear();
_q_textEdited(QString()); textEdited(QString());
} }
} }
void QLineEditPrivate::_q_controlEditingFinished() void QLineEditPrivate::controlEditingFinished()
{ {
Q_Q(QLineEdit); Q_Q(QLineEdit);
edited = false; edited = false;
@ -554,7 +577,8 @@ QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineE
if (!newAction) if (!newAction)
return nullptr; return nullptr;
if (!hasSideWidgets()) { // initial setup. if (!hasSideWidgets()) { // initial setup.
QObject::connect(q, SIGNAL(textChanged(QString)), q, SLOT(_q_textChanged(QString))); QObjectPrivate::connect(q, &QLineEdit::textChanged,
this, &QLineEditPrivate::textChanged);
lastTextSize = q->text().size(); lastTextSize = q->text().size();
} }
QWidget *w = nullptr; QWidget *w = nullptr;
@ -570,7 +594,8 @@ QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineE
toolButton->setIcon(newAction->icon()); toolButton->setIcon(newAction->icon());
toolButton->setOpacity(lastTextSize > 0 || !(flags & SideWidgetFadeInWithText) ? 1 : 0); toolButton->setOpacity(lastTextSize > 0 || !(flags & SideWidgetFadeInWithText) ? 1 : 0);
if (flags & SideWidgetClearButton) { if (flags & SideWidgetClearButton) {
QObject::connect(toolButton, SIGNAL(clicked()), q, SLOT(_q_clearButtonClicked())); QObjectPrivate::connect(toolButton, &QToolButton::clicked,
this, &QLineEditPrivate::clearButtonClicked);
#if QT_CONFIG(animation) #if QT_CONFIG(animation)
// The clear button is handled only by this widget. The button should be really // The clear button is handled only by this widget. The button should be really
@ -633,7 +658,8 @@ void QLineEditPrivate::removeAction(QAction *action)
delete entry.widget; delete entry.widget;
positionSideWidgets(); positionSideWidgets();
if (!hasSideWidgets()) // Last widget, remove connection if (!hasSideWidgets()) // Last widget, remove connection
QObject::disconnect(q, SIGNAL(textChanged(QString)), q, SLOT(_q_textChanged(QString))); QObjectPrivate::connect(q, &QLineEdit::textChanged,
this, &QLineEditPrivate::textChanged);
q->update(); q->update();
} }
#endif // QT_CONFIG(action) #endif // QT_CONFIG(action)

View File

@ -190,25 +190,27 @@ public:
QRect adjustedContentsRect() const; QRect adjustedContentsRect() const;
void _q_handleWindowActivate(); void handleWindowActivate();
void _q_textEdited(const QString &); void textEdited(const QString &);
void _q_cursorPositionChanged(int, int); void cursorPositionChanged(int, int);
#ifdef QT_KEYPAD_NAVIGATION #ifdef QT_KEYPAD_NAVIGATION
void _q_editFocusChange(bool); void editFocusChange(bool);
#endif #endif
void _q_selectionChanged(); void selectionChanged();
void _q_updateNeeded(const QRect &); void updateNeeded(const QRect &);
#if QT_CONFIG(completer) #if QT_CONFIG(completer)
void _q_completionHighlighted(const QString &); void connectCompleter();
void disconnectCompleter();
void completionHighlighted(const QString &);
#endif #endif
QPoint mousePressPos; QPoint mousePressPos;
#if QT_CONFIG(draganddrop) #if QT_CONFIG(draganddrop)
QBasicTimer dndTimer; QBasicTimer dndTimer;
void drag(); void drag();
#endif #endif
void _q_textChanged(const QString &); void textChanged(const QString &);
void _q_clearButtonClicked(); void clearButtonClicked();
void _q_controlEditingFinished(); void controlEditingFinished();
QMargins textMargins; // use effectiveTextMargins() in case of icon. QMargins textMargins; // use effectiveTextMargins() in case of icon.

View File

@ -516,6 +516,8 @@ private:
// accessibility events are sent for this object // accessibility events are sent for this object
QObject *m_accessibleObject; QObject *m_accessibleObject;
friend class QLineEdit;
}; };
QT_END_NAMESPACE QT_END_NAMESPACE