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. Saving QByteArray to text similar to QSettings' format in ini file?
Forum Updated to NodeBB v4.3 + New Features

Saving QByteArray to text similar to QSettings' format in ini file?

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 1.6k Views 2 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.
  • L lansing

    @lansing

    I want to save settings like QFont but storing its values separately seem unnecessary. Because when I load it back I would have to reconstruct it from all those values again before it's ready to use. So I think it would be easier to just wrap it in QByteArray for saving and retrieving.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by jsulm
    #5

    @lansing You should check documentation: https://doc-snapshots.qt.io/qt5-dev/qfont.html
    "If you want to store a user's font preferences you could use QSettings, writing the font information with toString() and reading it back with fromString(). The operator<<() and operator>>() functions are also available, but they work on a data stream."
    There is really no need for binary data.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    L 1 Reply Last reply
    3
    • jsulmJ jsulm

      @lansing You should check documentation: https://doc-snapshots.qt.io/qt5-dev/qfont.html
      "If you want to store a user's font preferences you could use QSettings, writing the font information with toString() and reading it back with fromString(). The operator<<() and operator>>() functions are also available, but they work on a data stream."
      There is really no need for binary data.

      L Offline
      L Offline
      lansing
      wrote on last edited by
      #6

      @jsulm

      I want to store a list of font as selection. User can add or remove font preference to this file

      [theme one]
      id_1 ; font-setting ; other stuff
      [theme two]
      id_2; font-setting; ...
      

      I don't think QSettings is right for this case?

      I also tried the operator<<() to store them separately as font family, font size etc but I have to reconstruct them back together when I loaded them, which is tedious.

      jsulmJ 1 Reply Last reply
      0
      • L lansing

        @jsulm

        I want to store a list of font as selection. User can add or remove font preference to this file

        [theme one]
        id_1 ; font-setting ; other stuff
        [theme two]
        id_2; font-setting; ...
        

        I don't think QSettings is right for this case?

        I also tried the operator<<() to store them separately as font family, font size etc but I have to reconstruct them back together when I loaded them, which is tedious.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #7

        @lansing said in Saving QByteArray to text similar to QSettings' format in ini file?:

        but I have to reconstruct them back together

        Why? fromString() should do it just fine if you stored it before using toString() as described in the documentation.

        "I want to store a list of font as selection" - you could construct this list by yourself calling toString() on each font and joining these strings into one using a separator. Later, when loading, just read the string from settings split it using the separator and use fromString() for each font. This is not that much work.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        L 1 Reply Last reply
        0
        • jsulmJ jsulm

          @lansing said in Saving QByteArray to text similar to QSettings' format in ini file?:

          but I have to reconstruct them back together

          Why? fromString() should do it just fine if you stored it before using toString() as described in the documentation.

          "I want to store a list of font as selection" - you could construct this list by yourself calling toString() on each font and joining these strings into one using a separator. Later, when loading, just read the string from settings split it using the separator and use fromString() for each font. This is not that much work.

          L Offline
          L Offline
          lansing
          wrote on last edited by
          #8

          @jsulm said in Saving QByteArray to text similar to QSettings' format in ini file?:

          fromString

          Oh I don't know that we can do that. What is the syntax of fromString() when reading back from the text?

          jsulmJ 1 Reply Last reply
          0
          • L lansing

            @jsulm said in Saving QByteArray to text similar to QSettings' format in ini file?:

            fromString

            Oh I don't know that we can do that. What is the syntax of fromString() when reading back from the text?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #9

            @lansing said in Saving QByteArray to text similar to QSettings' format in ini file?:

            What is the syntax of fromString() when reading back from the text?

            There is documentation: https://doc.qt.io/qt-5/qfont.html#fromString

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            L 1 Reply Last reply
            1
            • jsulmJ jsulm

              @lansing said in Saving QByteArray to text similar to QSettings' format in ini file?:

              What is the syntax of fromString() when reading back from the text?

              There is documentation: https://doc.qt.io/qt-5/qfont.html#fromString

              L Offline
              L Offline
              lansing
              wrote on last edited by
              #10

              @jsulm

              Yes I read it, this is what I got so far

              QFont preferenceFont;               
              preferenceFont.fromString(fontString);
              

              Is there a one liner for this? I tried QFont::fromString(fontString) but it doesn't work.

              jsulmJ J.HilkJ 2 Replies Last reply
              0
              • L lansing

                @jsulm

                Yes I read it, this is what I got so far

                QFont preferenceFont;               
                preferenceFont.fromString(fontString);
                

                Is there a one liner for this? I tried QFont::fromString(fontString) but it doesn't work.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #11

                @lansing said in Saving QByteArray to text similar to QSettings' format in ini file?:

                I tried QFont::fromString(fontString) but it doesn't work.

                Of course it does not since fromString() is not static.
                Why do you need one liner?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                L 1 Reply Last reply
                4
                • L lansing

                  @jsulm

                  Yes I read it, this is what I got so far

                  QFont preferenceFont;               
                  preferenceFont.fromString(fontString);
                  

                  Is there a one liner for this? I tried QFont::fromString(fontString) but it doesn't work.

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

                  @lansing

                  int main(int argc, char *argv[])
                  {
                      QApplication app(argc, argv);
                  
                      QFont defaultFont;
                      qDebug() << defaultFont;
                      defaultFont.setPixelSize(25);
                      defaultFont.setBold(true);
                      qDebug() << defaultFont;
                      QString stringRepresentation = defaultFont.toString();
                      qDebug() << stringRepresentation;
                      QFont loadedFont;
                      loadedFont.fromString(stringRepresentation);
                  
                      qDebug() << defaultFont << loadedFont;
                  //    return app.exec();
                  }
                  

                  output:

                  QFont( ".AppleSystemUIFont,13,-1,5,50,0,0,0,0,0" )
                  QFont( ".AppleSystemUIFont,-1,25,5,75,0,0,0,0,0" )
                  ".AppleSystemUIFont,-1,25,5,75,0,0,0,0,0"
                  QFont( ".AppleSystemUIFont,-1,25,5,75,0,0,0,0,0" ) QFont( ".AppleSystemUIFont,-1,25,5,75,0,0,0,0,0" )
                  

                  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
                  3
                  • jsulmJ jsulm

                    @lansing said in Saving QByteArray to text similar to QSettings' format in ini file?:

                    I tried QFont::fromString(fontString) but it doesn't work.

                    Of course it does not since fromString() is not static.
                    Why do you need one liner?

                    L Offline
                    L Offline
                    lansing
                    wrote on last edited by
                    #13

                    @jsulm

                    I will be passing it as an argument after the conversion, so it would be shorter to do it in one line instead of three.

                    QFont preferenceFont;               
                    preferenceFont.fromString(fontString);
                    document->setFont(preferenceFont)
                    
                    J.HilkJ 1 Reply Last reply
                    0
                    • L lansing

                      @jsulm

                      I will be passing it as an argument after the conversion, so it would be shorter to do it in one line instead of three.

                      QFont preferenceFont;               
                      preferenceFont.fromString(fontString);
                      document->setFont(preferenceFont)
                      
                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #14

                      @lansing

                      constexpr auto stringToFont = [](const QString &str)->QFont{
                              QFont preferenceFont;               
                              preferenceFont.fromString(str);
                              return preferenceFont;
                          };
                       document->setFont(stringToFont(fontString));
                      

                      made it constexpr to force inline-ness


                      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
                      4

                      • Login

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