Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Read/write user settings
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Read/write user settings

Scheduled Pinned Locked Moved QML and Qt Quick
17 Posts 5 Posters 11.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    strekazoid
    wrote on last edited by
    #3

    Thanks. So the scenario is to implement a method in main.cpp for reading / writing settings, and call it from QML side?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #4

      More or less. I use a thin wrapper around QSettings and expose it through a context property.
      @
      // settings.h

      class Settings : public QSettings
      {
      Q_OBJECT

      public:
      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);
      }
      }

      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        strekazoid
        wrote on last edited by
        #5

        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&#40;QLatin1String("qml/myApp/main.qml"&#41;&#41;;
        viewer->showExpanded();
        
        return app->exec&#40;&#41;;
        

        }@

        The only difference here with your code is that currently wizard generated code contains QmlApplicationViewer instance instead of QDeclarativeView (like in your code).

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #6

          You haven't included <QtGui/QDeclarativeContext>.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            strekazoid
            wrote on last edited by
            #7

            Thanks a lot, works now!

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #8

              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.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                strekazoid
                wrote on last edited by
                #9

                Hey, is there some way to save image to settings? I have path to the image, how do I save it now to Settings?

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rahul Das
                  wrote on last edited by
                  #10

                  Try this :
                  @settings.setValue("General/Pixmap", pixmap ); // pxmap is QPixmap@

                  I haven't tried it yet, let me knw whats happening :)


                  Declaration of (Platform) independence.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    strekazoid
                    wrote on last edited by
                    #11

                    Thanks. How do I construct QPixmap in QML?

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Rahul Das
                      wrote on last edited by
                      #12

                      "here":http://doc.qt.nokia.com/latest/qdeclarativeimageprovider.html


                      Declaration of (Platform) independence.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        strekazoid
                        wrote on last edited by
                        #13

                        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?

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          Rahul Das
                          wrote on last edited by
                          #14

                          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++.


                          Declaration of (Platform) independence.

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            strekazoid
                            wrote on last edited by
                            #15

                            Ok, thanks, I'll try to play with this idea.

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              Jon Heron
                              wrote on last edited by
                              #16

                              Thank you for this excellent post!!
                              Cheers,
                              Jon

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                Traxx
                                wrote on last edited by
                                #17

                                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

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved