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. Can't read custom type from QSettings
QtWS25 Last Chance

Can't read custom type from QSettings

Scheduled Pinned Locked Moved Solved General and Desktop
qt6.3.0qsettingsqvariantqdatastream
6 Posts 2 Posters 725 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.
  • J Offline
    J Offline
    jager012
    wrote on 16 Jun 2022, 10:15 last edited by jager012
    #1

    I am trying to read a small struct from my ini file using QSettings. Writing works fine, but when I try to read it back I always get the default value out of QVariant.

    This is the structure definition:

    struct CellSize {
        int font;
        int cell;
    };
    
    inline QDataStream& operator<<(QDataStream& out, const CharTableView::CellSize& v) {
        out << v.font << v.cell;
        return out;
    }
    
    inline QDataStream& operator>>(QDataStream& in, CharTableView::CellSize& v) {
        int font, cell;
        in >> font >> cell;
        v.font = font;
        v.cell = cell;
        return in;
    }
    
    inline bool operator==(const CharTableView::CellSize& a, const CharTableView::CellSize& b) {
        return a.font == b.font && a.cell == b.cell;
    }
    
    Q_DECLARE_METATYPE(CharTableView::CellSize)
    

    I am writing with m_settings.setValue(Settings::TableCellSize, QVariant::fromValue<CharTableView::CellSize>(CharTableView::CellSizeSmall));. I assume this is working fine because the gibberish inside the ini file is consistent with the UI changes. My reading code is CharTableView::CellSize tableCellSize = m_settings.value(Settings::TableCellSize).value<CharTableView::CellSize>(); and it always gives me {0, 0}.
    I have tried to set some breakpoints and, basically, the first breakpoint that gets triggered is the one after I've read the value from QSettings, which is {0, 0} as always. Then after a while a breakpoint inside operator>> gets triggered and the values of font and cell inside the operator function are correct. What's happening?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jager012
      wrote on 17 Jun 2022, 18:20 last edited by
      #5

      I solved the issue! I had to add qRegisterMetaType<CharTableView::CellSize>() inside CharTableView constructor.
      However, the documentation wasn't really clear on that matter, as it stated that Q_DECLARE_METATYPE() is enough for QVariant and you only need qRegisterMetaType<T>() to use custom types in signal-slot connections. The curious thing is that the data were actually being read correctly inside operator>> but, without the qRegisterMetaType<T>() magic, it happened only after the main window had already show up on screen. I wonder what was happening behind the scenes.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 16 Jun 2022, 18:25 last edited by
        #2

        Hi and welcome to devnet,

        Might be a silly question but are you sure the file loaded is the correct one when you read it ?

        Usually QSettings object are created within the method that will access them rather than as class members.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply 17 Jun 2022, 16:33
        1
        • S SGaist
          16 Jun 2022, 18:25

          Hi and welcome to devnet,

          Might be a silly question but are you sure the file loaded is the correct one when you read it ?

          Usually QSettings object are created within the method that will access them rather than as class members.

          J Offline
          J Offline
          jager012
          wrote on 17 Jun 2022, 16:33 last edited by
          #3

          @SGaist Thank you very much for your answer! Yes, the file is correct and I have no issue reading other settings (basic C++ types). This is the only value that I can't read. I'm gonna try declaring QSettings inside the function and see what happens.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 17 Jun 2022, 17:04 last edited by
            #4

            Try also inspecting the QVariant content using qDebug. You might want to implement the QDebug stream operator like you did for QDataStream.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • J Offline
              J Offline
              jager012
              wrote on 17 Jun 2022, 18:20 last edited by
              #5

              I solved the issue! I had to add qRegisterMetaType<CharTableView::CellSize>() inside CharTableView constructor.
              However, the documentation wasn't really clear on that matter, as it stated that Q_DECLARE_METATYPE() is enough for QVariant and you only need qRegisterMetaType<T>() to use custom types in signal-slot connections. The curious thing is that the data were actually being read correctly inside operator>> but, without the qRegisterMetaType<T>() magic, it happened only after the main window had already show up on screen. I wonder what was happening behind the scenes.

              1 Reply Last reply
              1
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 17 Jun 2022, 18:41 last edited by
                #6

                When you use custom types, it's pretty usual to register them all before building any of the widgets that will use them as part of your application startup sequence after you created your QApplication object.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                6/6

                17 Jun 2022, 18:41

                • Login

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