diff options
author | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2023-12-23 21:06:01 +0100 |
---|---|---|
committer | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2024-01-10 04:11:51 +0100 |
commit | bbdc8afa116155a9e8353f1d39af19a2228e3412 (patch) | |
tree | 62d6d8226bc18f6d3081bb8f470f5cd58651b670 | |
parent | 37e2aa7698a16fb5bc0d2f74388620d921d977c3 (diff) |
QLineEdit: Use pmf-style connects
Port all string-based signal/slots connections to pmf-style connects.
Pick-to: 6.7
Change-Id: I975232a3fedf82cd3327638a0ee119d1f2a90d84
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
-rw-r--r-- | src/widgets/widgets/qlineedit.cpp | 40 | ||||
-rw-r--r-- | src/widgets/widgets/qlineedit.h | 14 | ||||
-rw-r--r-- | src/widgets/widgets/qlineedit_p.cpp | 108 | ||||
-rw-r--r-- | src/widgets/widgets/qlineedit_p.h | 22 | ||||
-rw-r--r-- | src/widgets/widgets/qwidgetlinecontrol_p.h | 2 |
5 files changed, 99 insertions, 87 deletions
diff --git a/src/widgets/widgets/qlineedit.cpp b/src/widgets/widgets/qlineedit.cpp index 917a2b84d3..2513ebe634 100644 --- a/src/widgets/widgets/qlineedit.cpp +++ b/src/widgets/widgets/qlineedit.cpp @@ -622,7 +622,7 @@ void QLineEdit::setCompleter(QCompleter *c) if (c == d->control->completer()) return; if (d->control->completer()) { - disconnect(d->control->completer(), nullptr, this, nullptr); + d->disconnectCompleter(); d->control->completer()->setWidget(nullptr); if (d->control->completer()->parent() == this) delete d->control->completer(); @@ -632,12 +632,8 @@ void QLineEdit::setCompleter(QCompleter *c) return; if (c->widget() == nullptr) c->setWidget(this); - if (hasFocus()) { - QObject::connect(d->control->completer(), SIGNAL(activated(QString)), - this, SLOT(setText(QString))); - QObject::connect(d->control->completer(), SIGNAL(highlighted(QString)), - this, SLOT(_q_completionHighlighted(QString))); - } + if (hasFocus()) + d->connectCompleter(); } /*! @@ -1447,7 +1443,10 @@ bool QLineEdit::event(QEvent * e) #endif //d->separate(); } 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 } else if (e->type() == QEvent::ShortcutOverride) { QKeyEvent *ke = static_cast<QKeyEvent*>(e); @@ -1924,10 +1923,7 @@ void QLineEdit::focusInEvent(QFocusEvent *e) #if QT_CONFIG(completer) if (d->control->completer()) { d->control->completer()->setWidget(this); - QObject::connect(d->control->completer(), SIGNAL(activated(QString)), - this, SLOT(setText(QString))); - QObject::connect(d->control->completer(), SIGNAL(highlighted(QString)), - this, SLOT(_q_completionHighlighted(QString))); + d->connectCompleter(); } #endif update(); @@ -1966,9 +1962,8 @@ void QLineEdit::focusOutEvent(QFocusEvent *e) d->control->setCancelText(QString()); #endif #if QT_CONFIG(completer) - if (d->control->completer()) { - QObject::disconnect(d->control->completer(), nullptr, this, nullptr); - } + if (d->control->completer()) + d->disconnectCompleter(); #endif QWidget::focusOutEvent(e); } @@ -2215,12 +2210,12 @@ QMenu *QLineEdit::createStandardContextMenu() action = popup->addAction(QLineEdit::tr("&Undo") + ACCEL_KEY(QKeySequence::Undo)); action->setEnabled(d->control->isUndoAvailable()); 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->setEnabled(d->control->isRedoAvailable()); setActionIcon(action, QStringLiteral("edit-redo")); - connect(action, SIGNAL(triggered()), SLOT(redo())); + connect(action, &QAction::triggered, this, &QLineEdit::redo); popup->addSeparator(); } @@ -2231,20 +2226,20 @@ QMenu *QLineEdit::createStandardContextMenu() action->setEnabled(!d->control->isReadOnly() && d->control->hasSelectedText() && d->control->echoMode() == QLineEdit::Normal); 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->setEnabled(d->control->hasSelectedText() && d->control->echoMode() == QLineEdit::Normal); setActionIcon(action, QStringLiteral("edit-copy")); - connect(action, SIGNAL(triggered()), SLOT(copy())); + connect(action, &QAction::triggered, this, &QLineEdit::copy); if (!isReadOnly()) { action = popup->addAction(QLineEdit::tr("&Paste") + ACCEL_KEY(QKeySequence::Paste)); action->setEnabled(!d->control->isReadOnly() && !QGuiApplication::clipboard()->text().isEmpty()); setActionIcon(action, QStringLiteral("edit-paste")); - connect(action, SIGNAL(triggered()), SLOT(paste())); + connect(action, &QAction::triggered, this, &QLineEdit::paste); } #endif @@ -2252,7 +2247,8 @@ QMenu *QLineEdit::createStandardContextMenu() action = popup->addAction(QLineEdit::tr("Delete")); action->setEnabled(!d->control->isReadOnly() && !d->control->text().isEmpty() && d->control->hasSelectedText()); 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()) @@ -2262,7 +2258,7 @@ QMenu *QLineEdit::createStandardContextMenu() action->setEnabled(!d->control->text().isEmpty() && !d->control->allSelected()); setActionIcon(action, QStringLiteral("edit-select-all")); d->selectAllAction = action; - connect(action, SIGNAL(triggered()), SLOT(selectAll())); + connect(action, &QAction::triggered, this, &QLineEdit::selectAll); if (!d->control->isReadOnly() && QGuiApplication::styleHints()->useRtlExtensions()) { popup->addSeparator(); diff --git a/src/widgets/widgets/qlineedit.h b/src/widgets/widgets/qlineedit.h index e0729f0929..8955a67895 100644 --- a/src/widgets/widgets/qlineedit.h +++ b/src/widgets/widgets/qlineedit.h @@ -214,20 +214,6 @@ private: #endif Q_DISABLE_COPY(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 diff --git a/src/widgets/widgets/qlineedit_p.cpp b/src/widgets/widgets/qlineedit_p.cpp index d1b95da00a..3c49332429 100644 --- a/src/widgets/widgets/qlineedit_p.cpp +++ b/src/widgets/widgets/qlineedit_p.cpp @@ -74,8 +74,25 @@ QRect QLineEditPrivate::cursorRect() const } #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::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::_q_completionHighlighted(const QString &newText) +void QLineEditPrivate::completionHighlighted(const QString &newText) { Q_Q(QLineEdit); if (control->completer()->completionMode() != QCompleter::InlineCompletion) { @@ -96,14 +113,14 @@ void QLineEditPrivate::_q_completionHighlighted(const QString &newText) #endif // QT_CONFIG(completer) -void QLineEditPrivate::_q_handleWindowActivate() +void QLineEditPrivate::handleWindowActivate() { Q_Q(QLineEdit); if (!q->hasFocus() && control->hasSelectedText()) control->deselect(); } -void QLineEditPrivate::_q_textEdited(const QString &text) +void QLineEditPrivate::textEdited(const QString &text) { Q_Q(QLineEdit); edited = true; @@ -115,7 +132,7 @@ void QLineEditPrivate::_q_textEdited(const QString &text) #endif } -void QLineEditPrivate::_q_cursorPositionChanged(int from, int to) +void QLineEditPrivate::cursorPositionChanged(int from, int to) { Q_Q(QLineEdit); q->update(); @@ -123,14 +140,14 @@ void QLineEditPrivate::_q_cursorPositionChanged(int from, int to) } #ifdef QT_KEYPAD_NAVIGATION -void QLineEditPrivate::_q_editFocusChange(bool e) +void QLineEditPrivate::editFocusChange(bool e) { Q_Q(QLineEdit); q->setEditFocus(e); } #endif -void QLineEditPrivate::_q_selectionChanged() +void QLineEditPrivate::selectionChanged() { Q_Q(QLineEdit); if (control->preeditAreaText().isEmpty()) { @@ -150,7 +167,7 @@ void QLineEditPrivate::_q_selectionChanged() #endif } -void QLineEditPrivate::_q_updateNeeded(const QRect &rect) +void QLineEditPrivate::updateNeeded(const QRect &rect) { q_func()->update(adjustedControlRect(rect)); } @@ -158,45 +175,51 @@ void QLineEditPrivate::_q_updateNeeded(const QRect &rect) void QLineEditPrivate::init(const QString& txt) { Q_Q(QLineEdit); + + const auto qUpdateMicroFocus = [q]() + { + q->updateMicroFocus(); + }; control = new QWidgetLineControl(txt); control->setParent(q); control->setFont(q->font()); - QObject::connect(control, SIGNAL(textChanged(QString)), - q, SIGNAL(textChanged(QString))); - QObject::connect(control, SIGNAL(textEdited(QString)), - q, SLOT(_q_textEdited(QString))); - QObject::connect(control, SIGNAL(cursorPositionChanged(int,int)), - q, SLOT(_q_cursorPositionChanged(int,int))); - QObject::connect(control, SIGNAL(selectionChanged()), - q, SLOT(_q_selectionChanged())); - QObject::connect(control, SIGNAL(editingFinished()), - q, SLOT(_q_controlEditingFinished())); + QObject::connect(control, &QWidgetLineControl::textChanged, + q, &QLineEdit::textChanged); + QObjectPrivate::connect(control, &QWidgetLineControl::textEdited, + this, &QLineEditPrivate::textEdited); + QObjectPrivate::connect(control, &QWidgetLineControl::cursorPositionChanged, + this, &QLineEditPrivate::cursorPositionChanged); + QObjectPrivate::connect(control, &QWidgetLineControl::selectionChanged, + this, &QLineEditPrivate::selectionChanged); + QObjectPrivate::connect(control, &QWidgetLineControl::editingFinished, + this, &QLineEditPrivate::controlEditingFinished); #ifdef QT_KEYPAD_NAVIGATION - QObject::connect(control, SIGNAL(editFocusChange(bool)), - q, SLOT(_q_editFocusChange(bool))); + QObject::connect(control, &QWidgetLineControl::editFocusChange, + this, &QLineEditPrivate::editFocusChange); #endif - QObject::connect(control, SIGNAL(cursorPositionChanged(int,int)), - q, SLOT(updateMicroFocus())); + QObject::connect(control, &QWidgetLineControl::cursorPositionChanged, + q, qUpdateMicroFocus); - QObject::connect(control, SIGNAL(textChanged(QString)), - q, SLOT(updateMicroFocus())); + QObject::connect(control, &QWidgetLineControl::textChanged, + q, qUpdateMicroFocus); - QObject::connect(control, SIGNAL(updateMicroFocus()), - q, SLOT(updateMicroFocus())); + QObject::connect(control, &QWidgetLineControl::updateMicroFocus, + q, qUpdateMicroFocus); // for now, going completely overboard with updates. - QObject::connect(control, SIGNAL(selectionChanged()), - q, SLOT(update())); + QObject::connect(control, &QWidgetLineControl::selectionChanged, + q, qOverload<>(&QLineEdit::update)); - QObject::connect(control, SIGNAL(selectionChanged()), - q, SLOT(updateMicroFocus())); + QObject::connect(control, &QWidgetLineControl::selectionChanged, + q, qUpdateMicroFocus); - QObject::connect(control, SIGNAL(displayTextChanged(QString)), - q, SLOT(update())); + QObject::connect(control, &QWidgetLineControl::displayTextChanged, + q, qOverload<>(&QLineEdit::update)); - QObject::connect(control, SIGNAL(updateNeeded(QRect)), - q, SLOT(_q_updateNeeded(QRect))); - QObject::connect(control, SIGNAL(inputRejected()), q, SIGNAL(inputRejected())); + QObjectPrivate::connect(control, &QWidgetLineControl::updateNeeded, + this, &QLineEditPrivate::updateNeeded); + QObject::connect(control, &QWidgetLineControl::inputRejected, + q, &QLineEdit::inputRejected); QStyleOptionFrame opt; q->initStyleOption(&opt); @@ -436,7 +459,7 @@ static void displayWidgets(const QLineEditPrivate::SideWidgetEntryList &widgets, } #endif -void QLineEditPrivate::_q_textChanged(const QString &text) +void QLineEditPrivate::textChanged(const QString &text) { if (hasSideWidgets()) { 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); if (!q->text().isEmpty()) { q->clear(); - _q_textEdited(QString()); + textEdited(QString()); } } -void QLineEditPrivate::_q_controlEditingFinished() +void QLineEditPrivate::controlEditingFinished() { Q_Q(QLineEdit); edited = false; @@ -554,7 +577,8 @@ QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineE if (!newAction) return nullptr; 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(); } QWidget *w = nullptr; @@ -570,7 +594,8 @@ QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineE toolButton->setIcon(newAction->icon()); toolButton->setOpacity(lastTextSize > 0 || !(flags & SideWidgetFadeInWithText) ? 1 : 0); if (flags & SideWidgetClearButton) { - QObject::connect(toolButton, SIGNAL(clicked()), q, SLOT(_q_clearButtonClicked())); + QObjectPrivate::connect(toolButton, &QToolButton::clicked, + this, &QLineEditPrivate::clearButtonClicked); #if QT_CONFIG(animation) // 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; positionSideWidgets(); 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(); } #endif // QT_CONFIG(action) diff --git a/src/widgets/widgets/qlineedit_p.h b/src/widgets/widgets/qlineedit_p.h index 64350ae39a..3737ac8fe6 100644 --- a/src/widgets/widgets/qlineedit_p.h +++ b/src/widgets/widgets/qlineedit_p.h @@ -190,25 +190,27 @@ public: QRect adjustedContentsRect() const; - void _q_handleWindowActivate(); - void _q_textEdited(const QString &); - void _q_cursorPositionChanged(int, int); + void handleWindowActivate(); + void textEdited(const QString &); + void cursorPositionChanged(int, int); #ifdef QT_KEYPAD_NAVIGATION - void _q_editFocusChange(bool); + void editFocusChange(bool); #endif - void _q_selectionChanged(); - void _q_updateNeeded(const QRect &); + void selectionChanged(); + void updateNeeded(const QRect &); #if QT_CONFIG(completer) - void _q_completionHighlighted(const QString &); + void connectCompleter(); + void disconnectCompleter(); + void completionHighlighted(const QString &); #endif QPoint mousePressPos; #if QT_CONFIG(draganddrop) QBasicTimer dndTimer; void drag(); #endif - void _q_textChanged(const QString &); - void _q_clearButtonClicked(); - void _q_controlEditingFinished(); + void textChanged(const QString &); + void clearButtonClicked(); + void controlEditingFinished(); QMargins textMargins; // use effectiveTextMargins() in case of icon. diff --git a/src/widgets/widgets/qwidgetlinecontrol_p.h b/src/widgets/widgets/qwidgetlinecontrol_p.h index 838a46fff6..2ce0fe7d94 100644 --- a/src/widgets/widgets/qwidgetlinecontrol_p.h +++ b/src/widgets/widgets/qwidgetlinecontrol_p.h @@ -516,6 +516,8 @@ private: // accessibility events are sent for this object QObject *m_accessibleObject; + + friend class QLineEdit; }; QT_END_NAMESPACE |