Skip to content

Commit 23222a2

Browse files
committed
REF: Replace Q_FOREACH with range-based for loops
The use of Q_FOREACH is deprecated. Replaced instances of `foreach` with range-based `for` loops for modernization and code consistency.
1 parent c3558df commit 23222a2

13 files changed

+50
-47
lines changed

examples/PyLauncher/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int main( int argc, char **argv )
7878
}
7979
PythonQtScriptingConsole console(NULL, mainContext);
8080

81-
Q_FOREACH(QString file, files) {
81+
for( QString file : files ) {
8282
mainContext.evalFile(file);
8383
}
8484
if (showConsole || console.hadError()) {

generator/setupgenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static void addListRegistration(AbstractMetaType::shared_pointer type, QSet<QStr
182182

183183
/*
184184
QString debugStr;
185-
Q_FOREACH(AbstractMetaType* arg, args) {
185+
for( AbstractMetaType* arg : args ) {
186186
debugStr += QString(arg->typeEntry()->isEnum()?"ENUM ":"") + arg->typeEntry()->qualifiedCppName() + ",";
187187
if (arg->typeEntry()->qualifiedCppName() == "QPair") {
188188
debugStr += "(" + arg->instantiations().at(0)->typeEntry()->qualifiedCppName() + ",";

generator/shellimplgenerator.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_cla
8888

8989
IncludeList list = meta_class->typeEntry()->extraIncludes();
9090
std::sort(list.begin(), list.end());
91-
foreach (const Include &inc, list) {
91+
for (const Include &inc : list) {
9292
ShellGenerator::writeInclude(s, inc);
9393
}
9494
s << endl;
@@ -122,7 +122,7 @@ void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_cla
122122
s << "}" << endl;
123123

124124
AbstractMetaFunctionList virtualsForShell = getVirtualFunctionsForShell(meta_class);
125-
foreach (const AbstractMetaFunction *fun, virtualsForShell) {
125+
for (const AbstractMetaFunction *fun : virtualsForShell) {
126126
bool hasReturnValue = !fun->type().isNull();
127127
writeFunctionSignature(s, fun, meta_class, QString(),
128128
Option(ShowStatic | UnderscoreSpaces | UseIndexedName),
@@ -138,7 +138,11 @@ void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_cla
138138
s << "if (_wrapper) {" << endl;
139139
s << " PYTHONQT_GIL_SCOPE" << endl;
140140
s << " if (Py_REFCNT((PyObject*)_wrapper) > 0) {" << endl;
141+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
142+
s << " static PyObject* name = PyUnicode_FromString(\"" << fun->name() << "\");" << endl;
143+
#else
141144
s << " static PyObject* name = PyString_FromString(\"" << fun->name() << "\");" << endl;
145+
#endif
142146
s << " PyObject* obj = PyBaseObject_Type.tp_getattro((PyObject*)_wrapper, name);" << endl;
143147
s << " if (obj) {" << endl;
144148
s << " static const char* argumentList[] ={\"";
@@ -234,7 +238,7 @@ void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_cla
234238
if (meta_class->generateShellClass() || !meta_class->isAbstract()) {
235239

236240
// write constructors
237-
foreach (const AbstractMetaFunction *ctor, ctors) {
241+
for (const AbstractMetaFunction *ctor : ctors) {
238242
if (ctor->isAbstract() || (!meta_class->generateShellClass() && !ctor->isPublic())) { continue; }
239243

240244
s << meta_class->qualifiedCppName() << "* ";

src/PythonQt.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,7 @@ void PythonQtPrivate::setupSharedLibrarySuffixes()
15491549
_sharedLibrarySuffixes << "_d.so";
15501550
#endif
15511551
#endif
1552-
Q_FOREACH (QVariant entry, result.toList()) {
1552+
for( QVariant entry : result.toList( )) {
15531553
QVariantList suffixEntry = entry.toList();
15541554
if (suffixEntry.count()==3) {
15551555
int code = suffixEntry.at(2).toInt();
@@ -1622,7 +1622,7 @@ void PythonQtPrivate::addDecorators(QObject* o, int decoTypes)
16221622

16231623
void PythonQtPrivate::registerQObjectClassNames(const QStringList& names)
16241624
{
1625-
Q_FOREACH(QString name, names) {
1625+
for( QString name : names ) {
16261626
_knownQObjectClassNames.insert(name.toUtf8(), true);
16271627
}
16281628
}
@@ -1637,7 +1637,7 @@ void PythonQt::removeSignalHandlers()
16371637
QList<PythonQtSignalReceiver*> signalReceivers = _p->_signalReceivers.values();
16381638

16391639
// just delete all signal receivers, they will remove themselves via removeSignalEmitter()
1640-
foreach(PythonQtSignalReceiver* receiver, signalReceivers) {
1640+
for(PythonQtSignalReceiver* receiver : signalReceivers) {
16411641
delete receiver;
16421642
}
16431643
// just to be sure, clear the receiver map as well
@@ -2215,7 +2215,7 @@ PyObject* PythonQt::helpCalled(PythonQtClassInfo* info)
22152215

22162216
void PythonQt::clearNotFoundCachedMembers()
22172217
{
2218-
Q_FOREACH(PythonQtClassInfo* info, _p->_knownClassInfos) {
2218+
for( PythonQtClassInfo* info : _p->_knownClassInfos ) {
22192219
info->clearNotFoundCachedMembers();
22202220
}
22212221
}

src/PythonQtClassInfo.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ PythonQtClassInfo::~PythonQtClassInfo()
8686
if (_destructor) {
8787
_destructor->deleteOverloadsAndThis();
8888
}
89-
Q_FOREACH(PythonQtSlotInfo* info, _decoratorSlots) {
89+
for( PythonQtSlotInfo* info : _decoratorSlots ) {
9090
info->deleteOverloadsAndThis();
9191
}
9292
}
@@ -178,7 +178,7 @@ bool PythonQtClassInfo::lookForPropertyAndCache(const char* memberName)
178178
PythonQtSlotInfo* PythonQtClassInfo::recursiveFindDecoratorSlotsFromDecoratorProvider(const char* memberName, PythonQtSlotInfo* inputInfo, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset)
179179
{
180180
inputInfo = findDecoratorSlotsFromDecoratorProvider(memberName, inputInfo, found, memberCache, upcastingOffset);
181-
Q_FOREACH(const ParentClassInfo& info, _parentClasses) {
181+
for( const ParentClassInfo& info : _parentClasses ) {
182182
inputInfo = info._parent->recursiveFindDecoratorSlotsFromDecoratorProvider(memberName, inputInfo, found, memberCache, upcastingOffset+info._upcastingOffset);
183183
}
184184
return inputInfo;
@@ -333,7 +333,7 @@ PythonQtMemberInfo PythonQtClassInfo::member(const char* memberName)
333333
// look for dynamic decorators in this class and in derived classes
334334
QList<QObject*> decoObjects;
335335
recursiveCollectDecoratorObjects(decoObjects);
336-
Q_FOREACH(QObject* deco, decoObjects) {
336+
for( QObject* deco : decoObjects ) {
337337
// call on ourself for caching, but with different metaObject():
338338
found = lookForEnumAndCache(deco->metaObject(), memberName);
339339
if (found) {
@@ -352,7 +352,7 @@ PythonQtMemberInfo PythonQtClassInfo::member(const char* memberName)
352352
found = true;
353353
}
354354
// maybe it is a nested class?
355-
Q_FOREACH(PythonQtClassInfo* nestedClass, _nestedClasses) {
355+
for( PythonQtClassInfo* nestedClass : _nestedClasses ) {
356356
PyObject* pyClass = nestedClass->pythonQtClassWrapper();
357357
if (pyClass) {
358358
if (strcmp(memberName, nestedClass->unscopedClassName().constData()) == 0) {
@@ -394,14 +394,14 @@ void PythonQtClassInfo::recursiveCollectDecoratorObjects(QList<QObject*>& decora
394394
if (deco) {
395395
decoratorObjects.append(deco);
396396
}
397-
Q_FOREACH(const ParentClassInfo& info, _parentClasses) {
397+
for( const ParentClassInfo& info : _parentClasses ) {
398398
info._parent->recursiveCollectDecoratorObjects(decoratorObjects);
399399
}
400400
}
401401

402402
void PythonQtClassInfo::recursiveCollectClassInfos(QList<PythonQtClassInfo*>& classInfoObjects) {
403403
classInfoObjects.append(this);
404-
Q_FOREACH(const ParentClassInfo& info, _parentClasses) {
404+
for( const ParentClassInfo& info : _parentClasses ) {
405405
info._parent->recursiveCollectClassInfos(classInfoObjects);
406406
}
407407
}
@@ -496,7 +496,7 @@ QStringList PythonQtClassInfo::propertyList()
496496
}
497497
}
498498
QStringList members = memberList();
499-
foreach(QString member, members) {
499+
for(const QString& member : members) {
500500
if (member.startsWith("py_get_")) {
501501
l << member.mid(7);
502502
}
@@ -527,7 +527,7 @@ QStringList PythonQtClassInfo::memberList()
527527
// look for dynamic decorators in this class and in derived classes
528528
QList<PythonQtClassInfo*> infos;
529529
recursiveCollectClassInfos(infos);
530-
Q_FOREACH(PythonQtClassInfo* info, infos) {
530+
for( PythonQtClassInfo* info : infos ) {
531531
info->listDecoratorSlotsFromDecoratorProvider(l, false);
532532
}
533533
}
@@ -540,11 +540,11 @@ QStringList PythonQtClassInfo::memberList()
540540
// check enums in the class hierachy of CPP classes
541541
QList<QObject*> decoObjects;
542542
recursiveCollectDecoratorObjects(decoObjects);
543-
Q_FOREACH(QObject* deco, decoObjects) {
543+
for( QObject* deco : decoObjects ) {
544544
enumMetaObjects << deco->metaObject();
545545
}
546546

547-
Q_FOREACH(const QMetaObject* meta, enumMetaObjects) {
547+
for( const QMetaObject* meta : enumMetaObjects ) {
548548
for (int i = 0; i<meta->enumeratorCount(); i++) {
549549
QMetaEnum e = meta->enumerator(i);
550550
l << e.name();
@@ -554,7 +554,7 @@ QStringList PythonQtClassInfo::memberList()
554554
}
555555
}
556556

557-
Q_FOREACH(PythonQtClassInfo* nestedClass, _nestedClasses) {
557+
for( PythonQtClassInfo* nestedClass : _nestedClasses ) {
558558
PyObject* pyClass = nestedClass->pythonQtClassWrapper();
559559
if (pyClass) {
560560
QByteArray name = nestedClass->unscopedClassName();
@@ -583,7 +583,7 @@ void* PythonQtClassInfo::castTo(void* ptr, const char* classname)
583583
if (_wrappedClassName == classname) {
584584
return ptr;
585585
}
586-
Q_FOREACH(const ParentClassInfo& info, _parentClasses) {
586+
for( const ParentClassInfo& info : _parentClasses ) {
587587
void* result = info._parent->castTo((char*)ptr + info._upcastingOffset, classname);
588588
if (result) {
589589
return result;
@@ -597,7 +597,7 @@ bool PythonQtClassInfo::inherits(const char* name)
597597
if (_wrappedClassName == name) {
598598
return true;
599599
}
600-
Q_FOREACH(const ParentClassInfo& info, _parentClasses) {
600+
for( const ParentClassInfo& info : _parentClasses ) {
601601
if (info._parent->inherits(name)) {
602602
return true;
603603
}
@@ -610,7 +610,7 @@ bool PythonQtClassInfo::inherits(PythonQtClassInfo* classInfo)
610610
if (classInfo == this) {
611611
return true;
612612
}
613-
Q_FOREACH(const ParentClassInfo& info, _parentClasses) {
613+
for( const ParentClassInfo& info : _parentClasses ) {
614614
if (info._parent->inherits(classInfo)) {
615615
return true;
616616
}
@@ -767,14 +767,14 @@ QObject* PythonQtClassInfo::decorator()
767767
void* PythonQtClassInfo::recursiveCastDownIfPossible(void* ptr, const char** resultClassName)
768768
{
769769
if (!_polymorphicHandlers.isEmpty()) {
770-
Q_FOREACH(PythonQtPolymorphicHandlerCB* cb, _polymorphicHandlers) {
770+
for( PythonQtPolymorphicHandlerCB* cb : _polymorphicHandlers ) {
771771
void* resultPtr = (*cb)(ptr, resultClassName);
772772
if (resultPtr) {
773773
return resultPtr;
774774
}
775775
}
776776
}
777-
Q_FOREACH(const ParentClassInfo& info, _parentClasses) {
777+
for( const ParentClassInfo& info : _parentClasses ) {
778778
if (!info._parent->isQObject()) {
779779
void* resultPtr = info._parent->recursiveCastDownIfPossible((char*)ptr + info._upcastingOffset, resultClassName);
780780
if (resultPtr) {
@@ -818,7 +818,7 @@ void* PythonQtClassInfo::castDownIfPossible(void* ptr, PythonQtClassInfo** resul
818818
// we only do downcasting on the base object, not on the whole inheritance tree...
819819
void* resultPtr = nullptr;
820820
if (!_polymorphicHandlers.isEmpty()) {
821-
Q_FOREACH(PythonQtPolymorphicHandlerCB* cb, _polymorphicHandlers) {
821+
for( PythonQtPolymorphicHandlerCB* cb : _polymorphicHandlers ) {
822822
resultPtr = (*cb)(ptr, &className);
823823
if (resultPtr) {
824824
break;
@@ -897,7 +897,7 @@ void PythonQtClassInfo::createEnumWrappers(const QObject* decoratorProvider)
897897
if (decoratorProvider) {
898898
createEnumWrappers(decoratorProvider->metaObject());
899899
}
900-
Q_FOREACH(const ParentClassInfo& info, _parentClasses) {
900+
for( const ParentClassInfo& info : _parentClasses ) {
901901
// trigger decorator() instead of createEnumWrappers(),
902902
// which will then call createEnumWrappers().
903903
info._parent->decorator();
@@ -912,13 +912,13 @@ PyObject* PythonQtClassInfo::findEnumWrapper(const char* name) {
912912
// which will then call createEnumWrappers().
913913
decorator();
914914
}
915-
Q_FOREACH(const PythonQtObjectPtr& p, _enumWrappers) {
915+
for( const PythonQtObjectPtr& p : _enumWrappers ) {
916916
const char* className = ((PyTypeObject*)p.object())->tp_name;
917917
if (qstrcmp(className, name)==0) {
918918
return p.object();
919919
}
920920
}
921-
Q_FOREACH(const ParentClassInfo& info, _parentClasses) {
921+
for( const ParentClassInfo& info : _parentClasses ) {
922922
PyObject* p = info._parent->findEnumWrapper(name);
923923
if (p) return p;
924924
}

src/PythonQtClassWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
484484
QSet<QString> completeSet = QSet<QString>::fromList(members);
485485
completeSet.unite(QSet<QString>::fromList(properties));
486486
#endif
487-
Q_FOREACH (QString name, completeSet) {
487+
for( QString name : completeSet ) {
488488
if (name.startsWith("py_")) {
489489
// do not expose internal slots
490490
continue;

src/PythonQtConversion.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,8 +1365,8 @@ PyObject* PythonQtConv::QStringListToPyObject(const QStringList& list)
13651365
{
13661366
PyObject* result = PyTuple_New(list.count());
13671367
int i = 0;
1368-
QString str;
1369-
Q_FOREACH (str, list) {
1368+
1369+
for( QString str : list ) {
13701370
PyTuple_SET_ITEM(result, i, PythonQtConv::QStringToPyObject(str));
13711371
i++;
13721372
}
@@ -1432,8 +1432,7 @@ PyObject* PythonQtConv::QVariantHashToPyObject(const QVariantHash& m) {
14321432
PyObject* PythonQtConv::QVariantListToPyObject(const QVariantList& l) {
14331433
PyObject* result = PyTuple_New(l.count());
14341434
int i = 0;
1435-
QVariant v;
1436-
Q_FOREACH (v, l) {
1435+
for( QVariant v : l ) {
14371436
PyTuple_SET_ITEM(result, i, PythonQtConv::QVariantToPyObject(v));
14381437
i++;
14391438
}
@@ -1446,7 +1445,7 @@ PyObject* PythonQtConv::ConvertQListOfPointerTypeToPythonList(QList<void*>* list
14461445
{
14471446
PyObject* result = PyTuple_New(list->count());
14481447
int i = 0;
1449-
Q_FOREACH (void* value, *list) {
1448+
for( void* value : *list ) {
14501449
PyObject* wrap = PythonQt::priv()->wrapPtr(value, info.innerName);
14511450
if (wrap) {
14521451
PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)wrap;

src/PythonQtConversion.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ PyObject* PythonQtConvertListOfValueTypeToPythonList(const void* /*QList<T>* */
243243
}
244244
PyObject* result = PyTuple_New(list->size());
245245
int i = 0;
246-
Q_FOREACH (const T& value, *list) {
246+
for( const T& value : *list ) {
247247
PyTuple_SET_ITEM(result, i, PythonQtConv::convertQtValueToPythonInternal(innerType, &value));
248248
i++;
249249
}
@@ -293,7 +293,7 @@ PyObject* PythonQtConvertListOfKnownClassToPythonList(const void* /*QList<T>* */
293293
}
294294
PyObject* result = PyTuple_New(list->size());
295295
int i = 0;
296-
Q_FOREACH(const T& value, *list) {
296+
for( const T& value : *list ) {
297297
T* newObject = new T(value);
298298
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newObject, innerType->className());
299299
wrap->_ownedByPythonQt = true;
@@ -423,7 +423,7 @@ PyObject* PythonQtConvertListOfPairToPythonList(const void* /*QList<QPair<T1,T2>
423423
PyObject* result = PyTuple_New(list->size());
424424
int i = 0;
425425
typedef const QPair<T1, T2> Pair;
426-
Q_FOREACH(Pair& value, *list) {
426+
for( Pair& value : *list ) {
427427
PyObject* object = PythonQtConvertPairToPython<T1, T2>(&value, innerType);
428428
PyTuple_SET_ITEM(result, i, object);
429429
i++;

src/PythonQtImporter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ PythonQtImport::ModuleInfo PythonQtImport::getModuleInfo(PythonQtImporter* self,
101101

102102
QString test;
103103
// test if it is a shared library (they have precedence over *.py files and this is used in eggs)
104-
Q_FOREACH(const QString& suffix, PythonQt::priv()->sharedLibrarySuffixes()) {
104+
for( const QString& suffix : PythonQt::priv( )->sharedLibrarySuffixes()) {
105105
test = path + suffix;
106106
if (PythonQt::importInterface()->exists(test)) {
107107
info.fullPath = test;
@@ -143,7 +143,7 @@ int PythonQtImporter_init(PythonQtImporter *self, PyObject *args, PyObject * /*k
143143
return -1;
144144
} else {
145145
const QStringList& ignorePaths = PythonQt::self()->getImporterIgnorePaths();
146-
Q_FOREACH(QString ignorePath, ignorePaths) {
146+
for( QString ignorePath : ignorePaths ) {
147147
if (path.startsWith(ignorePath)) {
148148
PyErr_SetString(PythonQtImportError,
149149
"path ignored");

src/PythonQtInstanceWrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
392392
if (wrapper->_obj) {
393393
// we need to replace the properties with their real values...
394394
QStringList l = wrapper->classInfo()->propertyList();
395-
Q_FOREACH (QString name, l) {
395+
for( QString name : l ) {
396396
PyObject* o = PyObject_GetAttrString(obj, QStringToPythonConstCharPointer(name));
397397
if (o) {
398398
PyDict_SetItemString(dict, QStringToPythonConstCharPointer(name), o);
@@ -403,7 +403,7 @@ static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
403403
}
404404

405405
QList<QByteArray> dynamicProps = wrapper->_obj->dynamicPropertyNames();
406-
Q_FOREACH (QByteArray name, dynamicProps) {
406+
for( QByteArray name : dynamicProps ) {
407407
PyObject* o = PyObject_GetAttrString(obj, name.data());
408408
if (o) {
409409
PyDict_SetItemString(dict, name.data(), o);

0 commit comments

Comments
 (0)