From 1369bec9e26b66d6b129e0764774418a2e9a2e94 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Tue, 17 Jun 2025 09:10:45 +0200 Subject: [PATCH] TextEditor: optimize updating the paste action Selecting text with the mouse emits QClipboard::changed many times on systems that updates the clipboard on selection changes and checking whether the clipboard text is empty in the slot is potentially slow. Debounce calling the update paste action code to reduce checking the clipboard text. Fixes: QTCREATORBUG-33073 Change-Id: Ib8a6d4ed7a3688bc543e51ea7e9f012746c9630f Reviewed-by: Fabian Kosmale Reviewed-by: hjk --- src/plugins/texteditor/texteditor.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index c9c93e8b67f..5a515e36cac 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -1098,6 +1098,7 @@ public: QAction *m_jumpToFileAction = nullptr; QAction *m_jumpToFileInNextSplitAction = nullptr; QList m_modifyingActions; + bool m_updatePasteActionScheduled = false; }; class TextEditorWidgetFind : public BaseTextFind @@ -1281,7 +1282,16 @@ TextEditorWidgetPrivate::TextEditorWidgetPrivate(TextEditorWidget *parent) connect(q, &PlainTextEdit::copyAvailable, this, &TextEditorWidgetPrivate::updateCopyAction); - connect(qApp->clipboard(), &QClipboard::changed, this, &TextEditorWidgetPrivate::updatePasteAction); + connect(qApp->clipboard(), &QClipboard::dataChanged, this, [this] { + // selecting text with the mouse can cause the clipboard to change quite often and the + // check whether the clipboard text is empty in update paste action is potentially + // expensive. So we only schedule the update if it is not already scheduled. + + if (m_updatePasteActionScheduled) + return; + m_updatePasteActionScheduled = true; + QTimer::singleShot(100, this, &TextEditorWidgetPrivate::updatePasteAction); + }); m_parenthesesMatchingTimer.setSingleShot(true); m_parenthesesMatchingTimer.setInterval(50); @@ -4772,6 +4782,7 @@ void TextEditorWidgetPrivate::updateCopyAction(bool hasCopyableText) void TextEditorWidgetPrivate::updatePasteAction() { + m_updatePasteActionScheduled = false; if (m_pasteAction) m_pasteAction->setEnabled(!q->isReadOnly() && !qApp->clipboard()->text(QClipboard::Mode::Clipboard).isEmpty()); }