From 6e206704f93ce2d56aadaecdb2c140603b245cb2 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Wed, 1 Aug 2012 17:36:16 +0200 Subject: [PATCH] Make sure the clipboard is cleared before the QApplication instance is destroyed. Also add safety check so we don't try to use the clipboard if it's not available. --- src/gui/Clipboard.cpp | 21 ++++++++++++++------- src/gui/Clipboard.h | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/gui/Clipboard.cpp b/src/gui/Clipboard.cpp index 283c8d26d..8783c1b50 100644 --- a/src/gui/Clipboard.cpp +++ b/src/gui/Clipboard.cpp @@ -36,13 +36,7 @@ Clipboard::Clipboard(QObject* parent) { m_timer->setSingleShot(true); connect(m_timer, SIGNAL(timeout()), SLOT(clearClipboard())); -} - -Clipboard::~Clipboard() -{ - if (m_timer->isActive()) { - clearClipboard(); - } + connect(qApp, SIGNAL(aboutToQuit()), SLOT(cleanup())); } void Clipboard::setText(const QString& text) @@ -66,6 +60,11 @@ void Clipboard::clearClipboard() { QClipboard* clipboard = QApplication::clipboard(); + if (!clipboard) { + qWarning("Unable to access the clipboard."); + return; + } + clipboard->clear(QClipboard::Clipboard); if (clipboard->supportsSelection()) { clipboard->clear(QClipboard::Selection); @@ -77,6 +76,14 @@ void Clipboard::clearClipboard() #endif } +void Clipboard::cleanup() +{ + if (m_timer->isActive()) { + m_timer->stop(); + clearClipboard(); + } +} + Clipboard* Clipboard::instance() { if (!m_instance) { diff --git a/src/gui/Clipboard.h b/src/gui/Clipboard.h index 51ec73dc1..7c73ab343 100644 --- a/src/gui/Clipboard.h +++ b/src/gui/Clipboard.h @@ -29,13 +29,13 @@ class Clipboard : public QObject Q_OBJECT public: - ~Clipboard(); void setText(const QString& text); static Clipboard* instance(); private Q_SLOTS: void clearClipboard(); + void cleanup(); private: explicit Clipboard(QObject* parent = Q_NULLPTR);