Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
objectutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtWidgets>
8#ifdef QT_OPENGLWIDGETS_LIB
9#include <QtOpenGLWidgets/qopenglwidget.h>
10#endif
11
13
14#ifdef QFORMINTERNAL_NAMESPACE
15namespace QFormInternal
16{
17#endif
18
19template <typename ...T>
20struct TypeList {};
21
22using Layouts = TypeList<
23 QGridLayout
24 , QHBoxLayout
25 , QStackedLayout
26 , QVBoxLayout
27#if QT_CONFIG(formlayout)
28 , QFormLayout
29#endif
30 >;
31
32using Widgets = TypeList<
33 QWidget
34 , QDialog
35 , QDialogButtonBox
36 , QFrame
37 , QLabel
38#if QT_CONFIG(abstractslider)
39 , QAbstractSlider
40#endif
41#if QT_CONFIG(calendarwidget)
42 , QCalendarWidget
43#endif
44#if QT_CONFIG(checkbox)
45 , QCheckBox
46#endif
47#if QT_CONFIG(columnview)
48 , QColumnView
49#endif
50#if QT_CONFIG(combobox)
51 , QComboBox
52#endif
53#if QT_CONFIG(commandlinkbutton)
54 , QCommandLinkButton
55#endif
56#if QT_CONFIG(datetimeedit)
57 , QDateEdit
58 , QDateTimeEdit
59 , QTimeEdit
60#endif
61#if QT_CONFIG(dial)
62 , QDial
63#endif
64#if QT_CONFIG(dockwidget)
65 , QDockWidget
66#endif
67#if QT_CONFIG(fontcombobox)
68 , QFontComboBox
69#endif
70#if QT_CONFIG(groupbox)
71 , QGroupBox
72#endif
73#if QT_CONFIG(keysequenceedit)
74 , QKeySequenceEdit
75#endif
76#if QT_CONFIG(lcdnumber)
77 , QLCDNumber
78#endif
79#if QT_CONFIG(lineedit)
80 , QLineEdit
81#endif
82#if QT_CONFIG(listview)
83 , QListView
84#endif
85#if QT_CONFIG(listwidget)
86 , QListWidget
87#endif
88#if QT_CONFIG(mainwindow)
89 , QMainWindow
90#endif
91#if QT_CONFIG(mdiarea)
92 , QMdiArea
93#endif
94#if QT_CONFIG(menu)
95 , QMenu
96#endif
97#if QT_CONFIG(menubar)
98 , QMenuBar
99#endif
100#if QT_CONFIG(progressbar)
101 , QProgressBar
102#endif
103#if QT_CONFIG(pushbutton)
104 , QPushButton
105#endif
106#if QT_CONFIG(radiobutton)
107 , QRadioButton
108#endif
109#if QT_CONFIG(scrollarea)
110 , QAbstractScrollArea
111 , QScrollArea
112#endif
113#if QT_CONFIG(scrollbar)
114 , QScrollBar
115#endif
116#if QT_CONFIG(slider)
117 , QSlider
118#endif
119#if QT_CONFIG(spinbox)
120 , QAbstractSpinBox
121 , QDoubleSpinBox
122 , QSpinBox
123#endif
124#if QT_CONFIG(splitter)
125 , QSplitter
126#endif
127#if QT_CONFIG(stackedwidget)
128 , QStackedWidget
129#endif
130#if QT_CONFIG(statusbar)
131 , QStatusBar
132#endif
133#if QT_CONFIG(tableview)
134 , QTableView
135#endif
136#if QT_CONFIG(tablewidget)
137 , QTableWidget
138#endif
139#if QT_CONFIG(tabwidget)
140 , QTabWidget
141#endif
142#if QT_CONFIG(textbrowser)
143 , QTextBrowser
144#endif
145#if QT_CONFIG(textedit)
146 , QPlainTextEdit
147 , QTextEdit
148#endif
149#if QT_CONFIG(toolbar)
150 , QToolBar
151#endif
152#if QT_CONFIG(toolbox)
153 , QToolBox
154#endif
155#if QT_CONFIG(toolbutton)
156 , QToolButton
157#endif
158#if QT_CONFIG(treeview)
159 , QTreeView
160#endif
161#if QT_CONFIG(treewidget)
162 , QTreeWidget
163#endif
164#if QT_CONFIG(undoview)
165 , QUndoView
166#endif
167#if QT_CONFIG(wizard)
168 , QWizard
169 , QWizardPage
170#endif
171#ifdef QT_OPENGLWIDGETS_LIB
172 , QOpenGLWidget
173#endif
174#if !defined(QT_NO_GRAPHICSVIEW)
175 , QGraphicsView
176#endif
177 >;
178
179template <typename ...T>
181{
182 QStringList result;
183 // Fold expression: executes the lambda for each type in the list
184 ( [&result] { result.append(QString::fromUtf8(T::staticMetaObject.className())); }() , ... );
185 return result;
186}
187
189{
190 return objectNamesImpl(Layouts());
191}
192
194{
195 return objectNamesImpl(Widgets());
196}
197
198template <typename ...T>
200{
201 QList<const QMetaObject *> result;
202 // Fold expression: executes the lambda for each type in the list
203 ( [&result] { result.append(&T::staticMetaObject); }() , ... );
204 return result;
205}
206
208{
209 return metaObjectsImpl(Widgets());
210}
211
212template <typename Base, typename T>
213static Base *createObject(const QString &className, QWidget *parent)
214{
215 return className == QLatin1StringView(T::staticMetaObject.className()) ? new T(parent) : nullptr;
216}
217
218template <typename Base, typename ...T>
219static Base *createObjectImpl(const QString &className, QWidget *parent, TypeList<T...>)
220{
221 Base *result = nullptr;
222 // Left fold over the comma operator
223 // The expression inside the parenthesis runs for every type sequentially
224 ( (result == nullptr ? (result = createObject<Base, T>(className, parent)) : nullptr), ... );
225 return result;
226}
227
228QLayout *createLayoutInstance(const QString &className, QWidget *parent)
229{
230 return createObjectImpl<QLayout>(className, parent, Layouts());
231}
232
233QWidget *createWidgetInstance(const QString &className, QWidget *parent)
234{
235 return createObjectImpl<QWidget>(className, parent, Widgets());
236}
237
238#ifdef QFORMINTERNAL_NAMESPACE
239}
240#endif
241
242QT_END_NAMESPACE
The QLayout class is the base class of geometry managers.
Definition qlayout.h:27
friend class QWidget
Definition qpainter.h:432
Combined button and popup list for selecting options.
static Base * createObjectImpl(const QString &className, QWidget *parent, TypeList< T... >)
QStringList layoutNames()
static QStringList objectNamesImpl(TypeList< T... >)
static Base * createObject(const QString &className, QWidget *parent)
static QList< const QMetaObject * > metaObjectsImpl(TypeList< T... >)
QList< const QMetaObject * > widgetMetaObjects()
QWidget * createWidgetInstance(const QString &className, QWidget *parent)
QStringList widgetNames()
QLayout * createLayoutInstance(const QString &className, QWidget *parent)
QList< QString > QStringList
Constructs a string list that contains the given string, str.