Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Save and read enum by QSettings

Save and read enum by QSettings

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 990 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by sonichy
    #1
    *.h
    {
    private:
        enum {
            TEXT_CLOCK,
            DIGITAL_CLOCK,
            ANALOG_CLOCK
        } clock_type;
    }
    
    *.cpp
    {
        QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
        clock_type = settings.value("ClockType");
        if (clock_type == NULL)
            clock_type = TEXT_CLOCK;
        QAction *action_text = new QAction("Text", this);
        connect(action_text, &QAction::triggered, [=](){
            clock_type = TEXT_CLOCK;
            settings.setValue("ClockType", clock_type);
        });
        addAction(action_text);
    }
    
    J.HilkJ raven-worxR 2 Replies Last reply
    -3
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Is it so hard to write down what your problem is instead simply posting a code snippet?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      5
      • sonichyS sonichy
        *.h
        {
        private:
            enum {
                TEXT_CLOCK,
                DIGITAL_CLOCK,
                ANALOG_CLOCK
            } clock_type;
        }
        
        *.cpp
        {
            QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
            clock_type = settings.value("ClockType");
            if (clock_type == NULL)
                clock_type = TEXT_CLOCK;
            QAction *action_text = new QAction("Text", this);
            connect(action_text, &QAction::triggered, [=](){
                clock_type = TEXT_CLOCK;
                settings.setValue("ClockType", clock_type);
            });
            addAction(action_text);
        }
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @sonichy you did not specify a type for your enum, so it's an normal int (int32_t) simply cast it to and from the enum type.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        sonichyS 1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @sonichy you did not specify a type for your enum, so it's an normal int (int32_t) simply cast it to and from the enum type.

          sonichyS Offline
          sonichyS Offline
          sonichy
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • sonichyS sonichy
            *.h
            {
            private:
                enum {
                    TEXT_CLOCK,
                    DIGITAL_CLOCK,
                    ANALOG_CLOCK
                } clock_type;
            }
            
            *.cpp
            {
                QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
                clock_type = settings.value("ClockType");
                if (clock_type == NULL)
                    clock_type = TEXT_CLOCK;
                QAction *action_text = new QAction("Text", this);
                connect(action_text, &QAction::triggered, [=](){
                    clock_type = TEXT_CLOCK;
                    settings.setValue("ClockType", clock_type);
                });
                addAction(action_text);
            }
            
            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @sonichy said in Save and read enum by QSettings:

            clock_type = settings.value("ClockType");
            if (clock_type == NULL)
            

            If you look at the docs for QSettings::value() you will see that it returns a QVariant.
            So such a check doesnt make much sense.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2
            • sonichyS Offline
              sonichyS Offline
              sonichy
              wrote on last edited by
              #6
              enum ClockType {
                      TEXT_CLOCK,
                      DIGITAL_CLOCK,
                      ANALOG_CLOCK
              };
              ClockType clockType;
              
              settings.setValue("ClockType", clockType); //save
              clockType = static_cast<ClockType>(settings.value("ClockType").toInt()); //read
              
              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