Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqmlsettings.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
4#include "qqmlsettings_p.h"
5#include <qcoreevent.h>
6#include <qcoreapplication.h>
7#include <qloggingcategory.h>
8#include <qsettings.h>
9#include <qpointer.h>
10#include <qjsvalue.h>
11#include <qqmlinfo.h>
12#include <qdebug.h>
13#include <qhash.h>
14
16
34
204Q_LOGGING_CATEGORY(lcSettings, "qt.labs.settings")
205
206static const int settingsWriteDelay = 500;
207
209{
210 Q_DECLARE_PUBLIC(QQmlSettings)
211
212public:
214
216
217 void init();
218 void reset();
219
220 void load();
221 void store();
222
225
226 QQmlSettings *q_ptr = nullptr;
227 int timerId = 0;
228 bool initialized = false;
231 mutable QPointer<QSettings> settings;
232 QHash<const char *, QVariant> changedProperties;
233};
234
236
238{
239 if (!settings) {
240 QQmlSettings *q = const_cast<QQmlSettings*>(q_func());
241 settings = fileName.isEmpty() ? new QSettings(q) : new QSettings(fileName, QSettings::IniFormat, q);
243 // TODO: can't print out the enum due to the following error:
244 // error: C2666: 'QQmlInfo::operator <<': 15 overloads have similar conversions
245 qmlWarning(q) << "Failed to initialize QSettings instance. Status code is: " << int(settings->status());
246
248 QVector<QString> missingIdentifiers;
250 missingIdentifiers.append(QLatin1String("organizationName"));
252 missingIdentifiers.append(QLatin1String("organizationDomain"));
253 if (QCoreApplication::applicationName().isEmpty())
254 missingIdentifiers.append(QLatin1String("applicationName"));
255
256 if (!missingIdentifiers.isEmpty())
257 qmlWarning(q) << "The following application identifiers have not been set: " << missingIdentifiers;
258 }
259 return settings;
260 }
261
262 if (!category.isEmpty())
264 if (initialized)
265 q->d_func()->load();
266 }
267 return settings;
268}
269
271{
272 if (!initialized) {
273 qCDebug(lcSettings) << "QQmlSettings: stored at" << instance()->fileName();
274 load();
275 initialized = true;
276 }
277}
278
280{
282 store();
283 delete settings;
284}
285
287{
288 Q_Q(QQmlSettings);
289 const QMetaObject *mo = q->metaObject();
290 const int offset = mo->propertyOffset();
291 const int count = mo->propertyCount();
292
293 // don't save built-in properties if there aren't any qml properties
294 if (offset == 1)
295 return;
296
297 for (int i = offset; i < count; ++i) {
298 QMetaProperty property = mo->property(i);
299 const QString propertyName = QString::fromUtf8(property.name());
300
301 const QVariant previousValue = readProperty(property);
302 const QVariant currentValue = instance()->value(propertyName,
303 previousValue);
304
305 if (!currentValue.isNull() && (!previousValue.isValid()
306 || (currentValue.canConvert(previousValue.metaType())
307 && previousValue != currentValue))) {
308 property.write(q, currentValue);
309 qCDebug(lcSettings) << "QQmlSettings: load" << property.name() << "setting:" << currentValue << "default:" << previousValue;
310 }
311
312 // ensure that a non-existent setting gets written
313 // even if the property wouldn't change later
314 if (!instance()->contains(propertyName))
316
317 // setup change notifications on first load
318 if (!initialized && property.hasNotifySignal()) {
319 static const int propertyChangedIndex = mo->indexOfSlot("_q_propertyChanged()");
320 QMetaObject::connect(q, property.notifySignalIndex(), q, propertyChangedIndex);
321 }
322 }
323}
324
326{
328 while (it != changedProperties.constEnd()) {
329 instance()->setValue(QString::fromUtf8(it.key()), it.value());
330 qCDebug(lcSettings) << "QQmlSettings: store" << it.key() << ":" << it.value();
331 ++it;
332 }
334}
335
337{
338 Q_Q(QQmlSettings);
339 const QMetaObject *mo = q->metaObject();
340 const int offset = mo->propertyOffset();
341 const int count = mo->propertyCount();
342 for (int i = offset; i < count; ++i) {
343 const QMetaProperty &property = mo->property(i);
346 qCDebug(lcSettings) << "QQmlSettings: cache" << property.name() << ":" << value;
347 }
348 if (timerId != 0)
349 q->killTimer(timerId);
350 timerId = q->startTimer(settingsWriteDelay);
351}
352
354{
355 Q_Q(const QQmlSettings);
356 QVariant var = property.read(q);
357 if (var.metaType() == QMetaType::fromType<QJSValue>())
359 return var;
360}
361
363 : QObject(parent), d_ptr(new QQmlSettingsPrivate)
364{
365 Q_D(QQmlSettings);
366 d->q_ptr = this;
367}
368
370{
371 Q_D(QQmlSettings);
372 d->reset(); // flush pending changes
373}
374
383{
384 Q_D(const QQmlSettings);
385 return d->category;
386}
387
389{
390 Q_D(QQmlSettings);
391 if (d->category != category) {
392 d->reset();
393 d->category = category;
394 if (d->initialized)
395 d->load();
396 }
397}
398
410{
411 Q_D(const QQmlSettings);
412 return d->fileName;
413}
414
416{
417 Q_D(QQmlSettings);
418 if (d->fileName != fileName) {
419 d->reset();
420 d->fileName = fileName;
421 if (d->initialized)
422 d->load();
423 }
424}
425
436QVariant QQmlSettings::value(const QString &key, const QVariant &defaultValue) const
437{
438 Q_D(const QQmlSettings);
439 return d->instance()->value(key, defaultValue);
440}
441
453{
454 Q_D(const QQmlSettings);
455 d->instance()->setValue(key, value);
456 qCDebug(lcSettings) << "QQmlSettings: setValue" << key << ":" << value;
457}
458
473{
474 Q_D(QQmlSettings);
475 d->instance()->sync();
476}
477
479{
480}
481
483{
484 Q_D(QQmlSettings);
485 d->init();
486 qmlWarning(this) << "The Settings type from Qt.labs.settings is deprecated"
487 " and will be removed in a future release. Please use "
488 "the one from QtCore instead.";
489}
490
492{
493 Q_D(QQmlSettings);
494 if (event->timerId() == d->timerId) {
495 killTimer(d->timerId);
496 d->timerId = 0;
497
498 d->store();
499 }
501}
502
504
505#include "moc_qqmlsettings_p.cpp"
QString organizationName
the name of the organization that wrote this application
QString applicationName
the name of this application
QString organizationDomain
the Internet domain of the organization that wrote this application
\inmodule QtCore
Definition qhash.h:1145
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qhash.h:1219
const_iterator constBegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1215
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
Definition qhash.h:951
bool isEmpty() const noexcept
Returns true if the hash contains no items; otherwise returns false.
Definition qhash.h:928
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
The QJSValue class acts as a container for Qt/JavaScript data types.
Definition qjsvalue.h:31
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:103
virtual void timerEvent(QTimerEvent *event)
This event handler can be reimplemented in a subclass to receive timer events for the object.
Definition qobject.cpp:1470
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition qobject.cpp:1912
QPointer< QSettings > settings
QHash< const char *, QVariant > changedProperties
QQmlSettingsPrivate()=default
QVariant readProperty(const QMetaProperty &property) const
QSettings * instance() const
~QQmlSettings() override
void setFileName(const QString &fileName)
void classBegin() override
Invoked after class creation, but before any properties have been set.
void timerEvent(QTimerEvent *event) override
This event handler can be reimplemented in a subclass to receive timer events for the object.
Q_INVOKABLE void sync()
\qmlmethod Settings::sync()
Q_INVOKABLE void setValue(const QString &key, const QVariant &value)
\qmlmethod Settings::setValue(string key, var value)
void setCategory(const QString &category)
QQmlSettings(QObject *parent=nullptr)
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
Q_INVOKABLE QVariant value(const QString &key, const QVariant &defaultValue={}) const
\qmlmethod var Settings::value(string key, var defaultValue)
\inmodule QtCore
Definition qsettings.h:30
QString fileName() const
Returns the path where settings written using this QSettings object are stored.
void setValue(QAnyStringView key, const QVariant &value)
Sets the value of setting key to value.
QVariant value(QAnyStringView key, const QVariant &defaultValue) const
Returns the value for setting key.
@ AccessError
Definition qsettings.h:41
void beginGroup(QAnyStringView prefix)
Appends prefix to the current group.
Status status() const
Returns a status code indicating the first error that was met by QSettings, or QSettings::NoError if ...
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
\inmodule QtCore
Definition qcoreevent.h:366
\inmodule QtCore
Definition qvariant.h:65
T value() const &
Definition qvariant.h:516
QMetaType metaType() const
const QLoggingCategory & category()
[1]
QSet< QString >::iterator it
auto mo
[7]
static QT_BEGIN_NAMESPACE const int settingsWriteDelay
\qmlmodule Qt.labs.settings 1.0 \title Qt Labs Settings QML Types
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLuint64 key
GLenum GLenum GLsizei count
GLenum GLuint GLintptr offset
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
static QVariant toVariant(const QV4::Value &value, QMetaType typeHint, JSToQVariantConversionBehavior conversionBehavior, V4ObjectSet *visitedObjects)
const char property[13]
Definition qwizard.cpp:101
QSettings settings("MySoft", "Star Runner")
[0]
\inmodule QtCore
static Connection connect(const QObject *sender, int signal_index, const QObject *receiver, int method_index, int type=0, int *types=nullptr)
Definition qobject.cpp:3556