Skip to content

Commit 5fb9802

Browse files
committed
New utility class QgsTemporaryCursorRestoreOverride
This class temporarily removes ALL cursor overrides for the QApplication for the lifetime of the object, and restores them (in the same order) upon object destruction.
1 parent 6bb0723 commit 5fb9802

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/gui/qgsguiutils.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,31 @@ void QgsTemporaryCursorOverride::release()
272272
mHasOverride = false;
273273
QApplication::restoreOverrideCursor();
274274
}
275+
276+
277+
//
278+
// QgsTemporaryCursorRestoreOverride
279+
//
280+
281+
QgsTemporaryCursorRestoreOverride::QgsTemporaryCursorRestoreOverride()
282+
{
283+
while ( QApplication::overrideCursor() )
284+
{
285+
mCursors.emplace_back( QCursor( *QApplication::overrideCursor() ) );
286+
QApplication::restoreOverrideCursor();
287+
}
288+
}
289+
290+
QgsTemporaryCursorRestoreOverride::~QgsTemporaryCursorRestoreOverride()
291+
{
292+
restore();
293+
}
294+
295+
void QgsTemporaryCursorRestoreOverride::restore()
296+
{
297+
for ( auto it = mCursors.rbegin(); it != mCursors.rend(); ++it )
298+
{
299+
QApplication::setOverrideCursor( *it );
300+
}
301+
mCursors.clear();
302+
}

src/gui/qgsguiutils.h

+31
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,35 @@ class GUI_EXPORT QgsTemporaryCursorOverride
206206

207207
};
208208

209+
/**
210+
* Temporarily removes all cursor overrides for the QApplication for the lifetime of the object.
211+
*
212+
* When the object is deleted, all stacked cursor overrides are restored.
213+
*
214+
* \ingroup gui
215+
* \since QGIS 3.8
216+
*/
217+
class GUI_EXPORT QgsTemporaryCursorRestoreOverride
218+
{
219+
public:
220+
221+
/**
222+
* Constructor for QgsTemporaryCursorRestoreOverride. Removes all application override
223+
* cursors.
224+
*/
225+
QgsTemporaryCursorRestoreOverride();
226+
227+
~QgsTemporaryCursorRestoreOverride();
228+
229+
/**
230+
* Restores the cursor override early (i.e. before this object is destroyed).
231+
*/
232+
void restore();
233+
234+
private:
235+
236+
std::vector< QCursor > mCursors;
237+
238+
};
239+
209240
#endif // QGSGUIUTILS_H

0 commit comments

Comments
 (0)