[SOLVED] Read/write user settings
-
wrote on 17 Nov 2011, 08:11 last edited by
I need to save and read user settings for my QML application What would be the best way to do so? Some text/xml file? Is it possible to create/read/write text files from QML?
-
wrote on 17 Nov 2011, 08:21 last edited by
I usually expose a QSettings object from C++.
-
wrote on 17 Nov 2011, 08:38 last edited by
Thanks. So the scenario is to implement a method in main.cpp for reading / writing settings, and call it from QML side?
-
wrote on 17 Nov 2011, 09:13 last edited by
More or less. I use a thin wrapper around QSettings and expose it through a context property.
@
// settings.hclass Settings : public QSettings
{
Q_OBJECTpublic:
explicit Settings(QObject *parent = 0) : QSettings(QSettings::IniFormat,
QSettings::UserScope,
QCoreApplication::instance()->organizationName(),
QCoreApplication::instance()->applicationName(),
parent) {}Q_INVOKABLE inline void setValue(const QString &key, const QVariant &value) { QSettings::setValue(key, value); } Q_INVOKABLE inline QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const { return QSettings::value(key, defaultValue); }
};
Q_DECLARE_METATYPE(Settings*)
// main.cpp
QScopedPointer<QApplication> application(new QApplication(argc, argv));
Settings* settings = new Settings(application.data());QScopedPointer<QDeclarativeView> mainWindow(new QDeclarativeView);
mainWindow->rootContext()->setContextProperty("Settings", settings);// MainWindow.qml
Rectangle {
id: sidebarContent
width: Settings.value("desktop/MainWindow.sidebar.width", 200);
...
onReleased: {
Settings.setValue("desktop/MainWindow.sidebar.width", sidebarContent.width);
}
}@
-
wrote on 18 Nov 2011, 12:13 last edited by
Getting some troubles. Can you spot what is wrong here? I'm getting error: invalid use of incomplete type 'struct QDeclarativeContext' on line 12:
main.cpp
@#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "settings.h"Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());// Attaching QSettings wrapper and exposing it to our QML UI Settings* settings = new Settings(app.data()); viewer->rootContext()->setContextProperty("Settings", settings); viewer->setMainQmlFile(QLatin1String("qml/myApp/main.qml")); viewer->showExpanded(); return app->exec();
}@
The only difference here with your code is that currently wizard generated code contains QmlApplicationViewer instance instead of QDeclarativeView (like in your code).
-
wrote on 18 Nov 2011, 12:20 last edited by
You haven't included <QtGui/QDeclarativeContext>.
-
wrote on 18 Nov 2011, 12:27 last edited by
Thanks a lot, works now!
-
wrote on 18 Nov 2011, 12:40 last edited by
You're welcome. If the problem has been solved for you feel free to edit your initial post title and prepend "[Solved]" to it to indicate that there is a solution inside.
-
wrote on 22 Nov 2011, 12:18 last edited by
Hey, is there some way to save image to settings? I have path to the image, how do I save it now to Settings?
-
wrote on 22 Nov 2011, 12:40 last edited by
Try this :
@settings.setValue("General/Pixmap", pixmap ); // pxmap is QPixmap@I haven't tried it yet, let me knw whats happening :)
-
wrote on 22 Nov 2011, 12:46 last edited by
Thanks. How do I construct QPixmap in QML?
-
wrote on 22 Nov 2011, 12:48 last edited by
-
wrote on 22 Nov 2011, 12:51 last edited by
I'm still sort of confused. Is it possible to construct QPixmap in QML layer, or do I have to do something on C++ side as well?
-
wrote on 22 Nov 2011, 13:22 last edited by
A request from QML for image/pixmap is handled by the 'Image Provider ' class, in simple words.
Yes you have to implement the Image provider in C++.
-
wrote on 22 Nov 2011, 13:32 last edited by
Ok, thanks, I'll try to play with this idea.
-
wrote on 9 Jan 2012, 14:50 last edited by
Thank you for this excellent post!!
Cheers,
Jon -
wrote on 10 Feb 2014, 16:18 last edited by
Is the wrapper suppose to work out of the box and how do i make it work without qscopedpointer? Thank you
EDIT : Created a proper solution so anyone can answer to help future member