Cockatrice 2026-09-01-Development-3.1.0-beta.10
A virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
qt_utils.h
Go to the documentation of this file.
1#ifndef COCKATRICE_QT_UTILS_H
2#define COCKATRICE_QT_UTILS_H
3#include <QLayout>
4#include <QObject>
5
6namespace QtUtils
7{
8template <typename T> T *findParentOfType(const QObject *obj)
9{
10 const QObject *p = obj ? obj->parent() : nullptr;
11 while (p) {
12 if (auto casted = qobject_cast<T *>(const_cast<QObject *>(p))) {
13 return casted;
14 }
15 p = p->parent();
16 }
17 return nullptr;
18}
19
20static inline void clearLayoutRec(QLayout *l)
21{
22 if (!l) {
23 return;
24 }
25 QLayoutItem *it;
26 while ((it = l->takeAt(0)) != nullptr) {
27 if (QWidget *w = it->widget()) {
28 w->deleteLater();
29 }
30 if (QLayout *sub = it->layout()) {
31 clearLayoutRec(sub);
32 }
33 delete it;
34 }
35}
36} // namespace QtUtils
37
38#endif // COCKATRICE_QT_UTILS_H
Definition qt_utils.h:7
T * findParentOfType(const QObject *obj)
Definition qt_utils.h:8
static void clearLayoutRec(QLayout *l)
Definition qt_utils.h:20