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.
This commit is contained in:
Felix Geyer 2012-08-01 17:36:16 +02:00
parent 013a924e43
commit 6e206704f9
2 changed files with 15 additions and 8 deletions

View File

@ -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) {

View File

@ -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);