The only solution I found is with inheriting from QStringListModel.
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <QSettings> #include "MyStringListModel.h" #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); qmlRegisterType <MyStringListModel> ( "StringListModel", 1, 0, "MyStringListModel" ); MyStringListModel lstModel; QSettings settings ( "TestQml.ini", QSettings::IniFormat ); lstModel.readSettings(&settings, "Components"); if ( lstModel.stringList().size() < 1 ) { lstModel.setStringList( QStringList() << "Test 1" << "Test 2"); lstModel.setCurrentIndex(0); } qDebug() << lstModel.stringList(); engine.rootContext()->setContextProperty ("myLstModel", &lstModel ); engine.load(url); return app.exec(); }MyStringListModel.h
#ifndef MYSTRINGLISTMODEL_H #define MYSTRINGLISTMODEL_H #include <QObject> #include <QStringListModel> class QSettings; class MyStringListModel : public QStringListModel { Q_OBJECT Q_PROPERTY (int CurrentIndex READ getCurrentIndex WRITE setCurrentIndex NOTIFY sigCurrentIndex ) int CurrentIndex; public: MyStringListModel(); int getCurrentIndex () const; void setCurrentIndex ( int indx ); void readSettings ( QSettings *settings, const QString & category ); void writeSettings ( QSettings *settings, const QString & category ) const; public slots: void addNewText ( QString editText ); void sltEditText ( QString editText ); signals: void sigCurrentIndex(); }; #endif // MYSTRINGLISTMODEL_HMyStringListModel.cpp
#include "MyStringListModel.h" #include <QSettings> MyStringListModel::MyStringListModel() : CurrentIndex ( 0 ) { } int MyStringListModel::getCurrentIndex() const { return CurrentIndex; } void MyStringListModel::setCurrentIndex(int indx) { CurrentIndex = indx; } void MyStringListModel::addNewText(QString editText) { const QStringList &lst = stringList(); for ( int i = 0; i < lst.size(); ++i ) { if ( editText == lst[i] ) return; } setStringList ( stringList() << editText ); setCurrentIndex ( rowCount() - 1 ); } void MyStringListModel::readSettings(QSettings *settings, const QString &category) { settings->beginGroup(category); QStringList lst = settings->value("Entries", QStringList()).toStringList(); setStringList ( lst ); int currentIndex = settings->value("CurrentPosition", 0 ).toInt(); setCurrentIndex(currentIndex); settings->endGroup(); } void MyStringListModel::writeSettings(QSettings *settings, const QString &category) const { settings->beginGroup(category); settings->setValue("Entries", stringList()); settings->setValue("CurrentPosition", CurrentIndex ); settings->endGroup(); } void MyStringListModel::sltEditText(QString editText) { addNewText ( editText ); QSettings settings ( "TestQml.ini", QSettings::IniFormat ); writeSettings(&settings, "Components"); }main.qml
import QtQuick 2.15 import QtQuick.Controls 2.15 import StringListModel 1.0 ApplicationWindow { width: 640 height: 480 visible: true title: qsTr("Scroll") Frame { ComboBox { id: secondComboBox editable: true model: myLstModel currentIndex: model.CurrentIndex textRole: "display" onAccepted: { if (find(editText) === -1) model.addNewText ( editText ) } } } }