QSettings settings("myCompany", "myApp"); settings.beginWriteArray("myArray"); for (int i = 0; i < 5; ++i) { settings.setArrayIndex(i); settings.setValue("value", i * 2); } settings.endArray();
struct MyObject { QString name; int value; }; QSettings settings("myCompany", "myApp"); settings.beginWriteArray("myArray"); for (int i = 0; i < 3; ++i) { settings.setArrayIndex(i); MyObject object{QString("Object %1").arg(i), i * 10}; settings.setValue("name", object.name); settings.setValue("value", object.value); } settings.endArray();This code creates a struct called MyObject, which has two members: a QString called "name" and an int called "value". It then creates a new QSettings object and uses beginWriteArray to start writing an array to the settings file. In the loop, it creates a new MyObject with a unique name and value, sets the current array index, and writes the name and value members to separate keys using setValue. Finally, it calls endArray to close the array. These examples demonstrate how to use beginWriteArray to write arrays of primitive types or custom objects to a settings file using QSettings in C++. The Qt library provides a wide range of classes and methods for managing settings and configuration data in C++, and can be installed using the Qt package manager or downloaded from the Qt website.