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. Wrong enum value from QSettings using Qt 6
Forum Updated to NodeBB v4.3 + New Features

Wrong enum value from QSettings using Qt 6

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 6 Posters 4.7k Views
  • 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.
  • Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Please print a debug output of your QVariant to see if it's a QVariant conversion problem or QSettings.

    qDebug() << ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown);

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

    Cobra91151C 1 Reply Last reply
    2
    • Cobra91151C Cobra91151

      Hello!

      I have found an issue with my program using Qt 6. For example, I created this method to read the QSettings ini file.

      #define APP_SETTINGS_PATH QDir::toNativeSeparators(QString("%1/settings/%2.ini").arg(QApplication::applicationDirPath(), QString(APP_NAME).remove(QRegularExpression("\\s"))))
      
      QVariant ManageProgram::loadSettings(QString group, QString property, QVariant defaultValue)
      {
          QSettings appSettings(APP_SETTINGS_PATH, QSettings::IniFormat);
          appSettings.beginGroup(group);
          QVariant valueVariant = appSettings.value(property, defaultValue);
          appSettings.endGroup();
          return valueVariant;
      }
      

      Then, I want to get the value from my enum: Connection::State

      Connection::State wlanConnectionState = ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown).value<Connection::State>();
      

      INI file:

      [AppSettings]
      WlanState=2
      

      The stored value is: 2 but it returns as 0 on Qt 6.3.2, Qt 6.4.1, Qt 6.4.2, which breaks some of the features in my program. On Qt 5 this works well and returns the correct enum value: 2. Any ideas? Thank you.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #3

      @Cobra91151 said in Wrong enum value from QSettings using Qt 6:

      Connection::State

      Where is this enum in the docs, please? I can find enum QAbstractSocket::SocketState?

      Try reading your saved value as an int, do you get the right value? Try converting that to your desired enum, does that work?

      Cobra91151C 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Please print a debug output of your QVariant to see if it's a QVariant conversion problem or QSettings.

        qDebug() << ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown);

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by
        #4

        @Christian-Ehrlicher

        Ok. I have recompiled the program:

        qDebug() << ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown);
        It returns: QVariant(QString, "2")

        So, it seems it's QVariant conversion issue.

        1 Reply Last reply
        0
        • Cobra91151C Cobra91151

          Hello!

          I have found an issue with my program using Qt 6. For example, I created this method to read the QSettings ini file.

          #define APP_SETTINGS_PATH QDir::toNativeSeparators(QString("%1/settings/%2.ini").arg(QApplication::applicationDirPath(), QString(APP_NAME).remove(QRegularExpression("\\s"))))
          
          QVariant ManageProgram::loadSettings(QString group, QString property, QVariant defaultValue)
          {
              QSettings appSettings(APP_SETTINGS_PATH, QSettings::IniFormat);
              appSettings.beginGroup(group);
              QVariant valueVariant = appSettings.value(property, defaultValue);
              appSettings.endGroup();
              return valueVariant;
          }
          

          Then, I want to get the value from my enum: Connection::State

          Connection::State wlanConnectionState = ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown).value<Connection::State>();
          

          INI file:

          [AppSettings]
          WlanState=2
          

          The stored value is: 2 but it returns as 0 on Qt 6.3.2, Qt 6.4.1, Qt 6.4.2, which breaks some of the features in my program. On Qt 5 this works well and returns the correct enum value: 2. Any ideas? Thank you.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #5

          @Cobra91151 are you sure the Enum is correctly registered with the meta system ?

          I think Variant falls back on that.

          Besides that, simply read it as int:

          value<int>()
          

          and cast the result to the enum.


          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.

          1 Reply Last reply
          1
          • JonBJ JonB

            @Cobra91151 said in Wrong enum value from QSettings using Qt 6:

            Connection::State

            Where is this enum in the docs, please? I can find enum QAbstractSocket::SocketState?

            Try reading your saved value as an int, do you get the right value? Try converting that to your desired enum, does that work?

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #6

            @JonB

            No, it's my custom enum.

            namespace Connection {
                enum State {
                    NotConnected = 0,
                    Connecting = 1,
                    Connected = 2,
                    Canceled = 3,
                    Unknown = 4
                };
            }
            

            Yes, it works only when converting using toInt() method.

            int wlanState = ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown).toInt();
            wlanConnectionState = static_cast<Connection::State>(wlanState);
            qDebug() << "wlanConnectionState: " << wlanConnectionState;
            

            wlanConnectionState: 2

            Why it fails to convert to enum: wlanConnectionState = ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown).value<Connection::State>();? That's the question.

            Christian EhrlicherC 1 Reply Last reply
            0
            • Cobra91151C Cobra91151

              @JonB

              No, it's my custom enum.

              namespace Connection {
                  enum State {
                      NotConnected = 0,
                      Connecting = 1,
                      Connected = 2,
                      Canceled = 3,
                      Unknown = 4
                  };
              }
              

              Yes, it works only when converting using toInt() method.

              int wlanState = ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown).toInt();
              wlanConnectionState = static_cast<Connection::State>(wlanState);
              qDebug() << "wlanConnectionState: " << wlanConnectionState;
              

              wlanConnectionState: 2

              Why it fails to convert to enum: wlanConnectionState = ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown).value<Connection::State>();? That's the question.

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @Cobra91151 said in Wrong enum value from QSettings using Qt 6:

              Why it fails to convert to enum

              @J-Hilk

              are you sure the Enum is correctly registered with the meta system ?

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

              Cobra91151C 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @Cobra91151 said in Wrong enum value from QSettings using Qt 6:

                Why it fails to convert to enum

                @J-Hilk

                are you sure the Enum is correctly registered with the meta system ?

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by
                #8

                @J-Hilk
                @Christian-Ehrlicher
                @JonB

                I think, it is already registered: Q_DECLARE_METATYPE(Connection::State).
                Or Qt 6 uses different method of registering types?

                J.HilkJ 1 Reply Last reply
                0
                • Cobra91151C Cobra91151

                  @J-Hilk
                  @Christian-Ehrlicher
                  @JonB

                  I think, it is already registered: Q_DECLARE_METATYPE(Connection::State).
                  Or Qt 6 uses different method of registering types?

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #9

                  @Cobra91151 did you also use Q_ENUM_NS or Q_ENUM if your declaration is inside a QObject class?


                  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.

                  1 Reply Last reply
                  0
                  • Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by
                    #10

                    @J-Hilk

                    This enum declaration is in constants.h (header file). I use it as a helper file to create some enums/structs/defines there and use it in my program. There is no class in this file.
                    So, it's outside of a QObject class.

                    1 Reply Last reply
                    0
                    • Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by Cobra91151
                      #11

                      I think that I need to use something like this in my program.

                      #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
                          int wlanState = ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown).toInt();
                          wlanConnectionState = static_cast<Connection::State>(wlanState);
                      #else
                          wlanConnectionState = ManageProgram::loadSettings("AppSettings", "WlanState", Connection::State::Unknown).value<Connection::State>();
                      #endif
                      

                      The output for both cases: wlanConnectionState: 2

                      I should use the .value<Connection::State>(); in Qt 5 as always. And use .toInt() and then cast the result to the enum in Qt 6 until QVariant conversion problem is fixed.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        Works fine for me with Qt6.head + MSVC

                        namespace Connection {
                            enum State {
                                NotConnected = 0,
                                Connecting = 1,
                                Connected = 2,
                                Canceled = 3,
                                Unknown = 4
                            };
                        }
                        Q_DECLARE_METATYPE(Connection::State);  // not even needed for the testcase
                        
                        int main(int argc, char* argv[])
                        {
                            QCoreApplication app(argc, argv);
                            QVariant v(2);
                            Connection::State s = v.value<Connection::State>();
                            if (s == Connection::Connected) 
                                qDebug() << "Connected";
                            else 
                                qDebug() << "Not connected";
                        }
                        

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

                        Cobra91151C 2 Replies Last reply
                        1
                        • Christian EhrlicherC Christian Ehrlicher

                          Works fine for me with Qt6.head + MSVC

                          namespace Connection {
                              enum State {
                                  NotConnected = 0,
                                  Connecting = 1,
                                  Connected = 2,
                                  Canceled = 3,
                                  Unknown = 4
                              };
                          }
                          Q_DECLARE_METATYPE(Connection::State);  // not even needed for the testcase
                          
                          int main(int argc, char* argv[])
                          {
                              QCoreApplication app(argc, argv);
                              QVariant v(2);
                              Connection::State s = v.value<Connection::State>();
                              if (s == Connection::Connected) 
                                  qDebug() << "Connected";
                              else 
                                  qDebug() << "Not connected";
                          }
                          
                          Cobra91151C Offline
                          Cobra91151C Offline
                          Cobra91151
                          wrote on last edited by Cobra91151
                          #13

                          @Christian-Ehrlicher

                          Yes, it works. So, the enum must be overwritten somewhere in my program which changes the value to 0.
                          I will check it and fix it. Thank you very much for checking it.

                          1 Reply Last reply
                          1
                          • Christian EhrlicherC Christian Ehrlicher

                            Works fine for me with Qt6.head + MSVC

                            namespace Connection {
                                enum State {
                                    NotConnected = 0,
                                    Connecting = 1,
                                    Connected = 2,
                                    Canceled = 3,
                                    Unknown = 4
                                };
                            }
                            Q_DECLARE_METATYPE(Connection::State);  // not even needed for the testcase
                            
                            int main(int argc, char* argv[])
                            {
                                QCoreApplication app(argc, argv);
                                QVariant v(2);
                                Connection::State s = v.value<Connection::State>();
                                if (s == Connection::Connected) 
                                    qDebug() << "Connected";
                                else 
                                    qDebug() << "Not connected";
                            }
                            
                            Cobra91151C Offline
                            Cobra91151C Offline
                            Cobra91151
                            wrote on last edited by Cobra91151
                            #14

                            @Christian-Ehrlicher
                            @JonB
                            @J-Hilk

                            I have commented all wlanConnectionState code in my program where it could be overwritten, recompiled it (clean/run qmake/build) but it still returns as 0. So, I have used @Christian-Ehrlicher example and added my code there.

                            #include <QCoreApplication>
                            #include <QVariant>
                            #include <QSettings>
                            #include <QDir>
                            #include <QRegularExpression>
                            
                            #define APP_NAME "ConsoleApp"
                            #define APP_SETTINGS_PATH QDir::toNativeSeparators(QString("%1/settings/%2.ini").arg(QCoreApplication::applicationDirPath(), QString(APP_NAME).remove(QRegularExpression("\\s"))))
                            
                            namespace Connection {
                                enum State {
                                    NotConnected = 0,
                                    Connecting = 1,
                                    Connected = 2,
                                    Canceled = 3,
                                    Unknown = 4
                                };
                            }
                            
                            Q_DECLARE_METATYPE(Connection::State);  // not even needed for the testcase
                            QVariant loadSettings(QString group, QString property, QVariant defaultValue);
                            
                            int main(int argc, char* argv[])
                            {
                                QCoreApplication app(argc, argv);
                                //QVariant v(2);
                                Connection::State s = loadSettings("AppSettings", "WlanState", Connection::State::Unknown).value<Connection::State>();
                            
                                //int wlanState = loadSettings("AppSettings", "WlanState", Connection::State::Unknown).toInt();
                                //Connection::State s = static_cast<Connection::State>(wlanState);
                            
                                if (s == Connection::Connected) {
                                    qDebug() << "Connected";
                                } else {
                                    qDebug() << "Not connected";
                                }
                            
                                qDebug() << "s: " << s;
                            }
                            
                            QVariant loadSettings(QString group, QString property, QVariant defaultValue)
                            {
                                QSettings appSettings(APP_SETTINGS_PATH, QSettings::IniFormat);
                                appSettings.beginGroup(group);
                                QVariant valueVariant = appSettings.value(property, defaultValue);
                                appSettings.endGroup();
                                return valueVariant;
                            }
                            

                            It also returns:

                            Not connected
                            s:  0
                            

                            When I use this code:

                            int wlanState = loadSettings("AppSettings", "WlanState", Connection::State::Unknown).toInt();
                            Connection::State s = static_cast<Connection::State>(wlanState);
                            

                            The output is the following:

                            Connected
                            s:  2
                            

                            So, it seems that issue with QVariant conversion still exists.
                            Can you confirm (reproduce) it using updated code? Also, you will need the settings directory with .ini file: https://mega.nz/file/EVRwWI5b#4MXbcgqE1aCm_YKEk0leZnMnEGMWw1yuLHL61UlIi54

                            Or create one called ConsoleApp.ini with the following content:

                            [AppSettings]
                            WlanState=2
                            

                            Thank you.

                            1 Reply Last reply
                            0
                            • JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on last edited by
                              #15

                              @Cobra91151 said in Wrong enum value from QSettings using Qt 6:

                              namespace Connection {
                              enum State {
                              NotConnected = 0,
                              Connecting = 1,
                              Connected = 2,
                              Canceled = 3,
                              Unknown = 4
                              };
                              }

                              why not use enum class?

                              namespace Connection {
                                  enum class State {
                                      NotConnected = 0,
                                      Connecting = 1,
                                      Connected = 2,
                                      Canceled = 3,
                                      Unknown = 4
                                  };
                              }
                              
                              Cobra91151C 1 Reply Last reply
                              0
                              • JoeCFDJ JoeCFD

                                @Cobra91151 said in Wrong enum value from QSettings using Qt 6:

                                namespace Connection {
                                enum State {
                                NotConnected = 0,
                                Connecting = 1,
                                Connected = 2,
                                Canceled = 3,
                                Unknown = 4
                                };
                                }

                                why not use enum class?

                                namespace Connection {
                                    enum class State {
                                        NotConnected = 0,
                                        Connecting = 1,
                                        Connected = 2,
                                        Canceled = 3,
                                        Unknown = 4
                                    };
                                }
                                
                                Cobra91151C Offline
                                Cobra91151C Offline
                                Cobra91151
                                wrote on last edited by
                                #16

                                @JoeCFD

                                Hello!

                                I do not think it will work in my case, since I need to store enum value as integer in the .ini file and get it later as enum. On Qt 5 it works as expected. So, I think there is an issue with enum convertion in Qt 6. You can try out my example above to see what the issue is.
                                Anyway, thank you for your reply :)

                                1 Reply Last reply
                                0
                                • Christian EhrlicherC Online
                                  Christian EhrlicherC Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by Christian Ehrlicher
                                  #17

                                  Ok, looks like an enum now need Q_ENUM() (even though I think it may be a subtle bug):

                                  struct Connection {
                                      Q_GADGET
                                  public:
                                          enum State {
                                          NotConnected = 0,
                                          Connecting = 1,
                                          Connected = 2,
                                          Canceled = 3,
                                          Unknown = 4
                                      };
                                      Q_ENUM(State)
                                  };
                                  

                                  This also allows this in your ini file:

                                  [AppSettings]
                                  WlanState=Connected
                                  

                                  Will investigate it later on

                                  // see https://bugreports.qt.io/browse/QTBUG-109744

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

                                  J.HilkJ 1 Reply Last reply
                                  3
                                  • H Offline
                                    H Offline
                                    henrycavill23
                                    Banned
                                    wrote on last edited by
                                    #18
                                    This post is deleted!
                                    1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Christian Ehrlicher

                                      Ok, looks like an enum now need Q_ENUM() (even though I think it may be a subtle bug):

                                      struct Connection {
                                          Q_GADGET
                                      public:
                                              enum State {
                                              NotConnected = 0,
                                              Connecting = 1,
                                              Connected = 2,
                                              Canceled = 3,
                                              Unknown = 4
                                          };
                                          Q_ENUM(State)
                                      };
                                      

                                      This also allows this in your ini file:

                                      [AppSettings]
                                      WlanState=Connected
                                      

                                      Will investigate it later on

                                      // see https://bugreports.qt.io/browse/QTBUG-109744

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #19

                                      @Christian-Ehrlicher

                                      @J-Hilk said in Wrong enum value from QSettings using Qt 6:

                                      @Cobra91151 did you also use Q_ENUM_NS or Q_ENUM if your declaration is inside a QObject class?

                                      cough 🤓

                                      might be in it since the beginning of Qt6 ? because I don't remember it ever working without the Q_ENUM macro?


                                      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.

                                      Cobra91151C Christian EhrlicherC 2 Replies Last reply
                                      0
                                      • J.HilkJ J.Hilk

                                        @Christian-Ehrlicher

                                        @J-Hilk said in Wrong enum value from QSettings using Qt 6:

                                        @Cobra91151 did you also use Q_ENUM_NS or Q_ENUM if your declaration is inside a QObject class?

                                        cough 🤓

                                        might be in it since the beginning of Qt6 ? because I don't remember it ever working without the Q_ENUM macro?

                                        Cobra91151C Offline
                                        Cobra91151C Offline
                                        Cobra91151
                                        wrote on last edited by
                                        #20

                                        @Christian-Ehrlicher @J-Hilk

                                        I see this bug is fixed in Qt 6.5.0 & Qt 6.6.0. The issue is resolved.
                                        Thank you.

                                        1 Reply Last reply
                                        1
                                        • Cobra91151C Cobra91151 has marked this topic as solved on
                                        • J.HilkJ J.Hilk

                                          @Christian-Ehrlicher

                                          @J-Hilk said in Wrong enum value from QSettings using Qt 6:

                                          @Cobra91151 did you also use Q_ENUM_NS or Q_ENUM if your declaration is inside a QObject class?

                                          cough 🤓

                                          might be in it since the beginning of Qt6 ? because I don't remember it ever working without the Q_ENUM macro?

                                          Christian EhrlicherC Online
                                          Christian EhrlicherC Online
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #21

                                          @J-Hilk said in Wrong enum value from QSettings using Qt 6:

                                          because I don't remember it ever working without the Q_ENUM macro?

                                          It worked in Qt5 so it's a Qt6 regression. And it is very surprising because QVariant<int>() properly converts to an enum whereas QVariant<QString>() did not.

                                          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
                                          0
                                          • JonBJ JonB referenced this topic on

                                          • Login

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