GxtWaitCursor:Qt下基于RAII的鼠标等待光标类
#include <QTimer>
#include <QCursor>
#include <QGuiApplication>
#include "GxtWaitCursor.h"
//=======================================================================================
GxtWaitCursor::GxtWaitCursor(QObject *parent)
: QObject(parent)
{
beginWait();
}
//=======================================================================================
GxtWaitCursor::GxtWaitCursor(int msec, QObject *parent)
: QObject(parent)
{
beginWait(msec);
}
//=======================================================================================
GxtWaitCursor::~GxtWaitCursor()
{
endWait();
}
//=======================================================================================
void GxtWaitCursor::beginWait(int msec)
{
m_cursorHasRestored = false;
QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
if (msec > 0)
{
QTimer::singleShot(msec, [this]
{
if (!m_cursorHasRestored)
{
QGuiApplication::restoreOverrideCursor();
m_cursorHasRestored = true;
}
});
}
}
//=======================================================================================
void GxtWaitCursor::endWait()
{
if (!m_cursorHasRestored)
{
QGuiApplication::restoreOverrideCursor();
m_cursorHasRestored = true;
}
}