Utils: Add a convenience function to fill clipboard and selection

Relieves the user code side from repeated code which then tends to
get forgotten.

Change-Id: I079f97a658b55f3c79111df1946b8d72863ce513
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2022-06-16 13:53:26 +02:00
parent a13a83b65d
commit 74e35d68a8
2 changed files with 25 additions and 0 deletions

View File

@@ -29,6 +29,11 @@
#include "hostosinfo.h" #include "hostosinfo.h"
#include "qtcassert.h" #include "qtcassert.h"
#ifdef QT_WIDGETS_LIB
#include <QApplication>
#include <QClipboard>
#endif
#include <QCoreApplication> #include <QCoreApplication>
#include <QDir> #include <QDir>
#include <QJsonArray> #include <QJsonArray>
@@ -469,4 +474,16 @@ QTCREATOR_UTILS_EXPORT QString languageNameFromLanguageCode(const QString &langu
return languageName; return languageName;
} }
#ifdef QT_WIDGETS_LIB
QTCREATOR_UTILS_EXPORT void setClipboardAndSelection(const QString &text)
{
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(text);
if (clipboard->supportsSelection())
clipboard->setText(text, QClipboard::Selection);
}
#endif
} // namespace Utils } // namespace Utils

View File

@@ -122,4 +122,12 @@ QTCREATOR_UTILS_EXPORT QString wildcardToRegularExpression(const QString &origin
QTCREATOR_UTILS_EXPORT QString languageNameFromLanguageCode(const QString &languageCode); QTCREATOR_UTILS_EXPORT QString languageNameFromLanguageCode(const QString &languageCode);
#ifdef QT_WIDGETS_LIB
// Feeds the global clipboard and, when present, the primary selection
QTCREATOR_UTILS_EXPORT void setClipboardAndSelection(const QString &text);
#endif
} // namespace Utils } // namespace Utils