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. Storing QTextCharFormat and QColor to text file?
Forum Updated to NodeBB v4.3 + New Features

Storing QTextCharFormat and QColor to text file?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtextcharformatqcolorconvert
7 Posts 2 Posters 1.1k 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.
  • L Offline
    L Offline
    lansing
    wrote on 17 Jul 2020, 00:41 last edited by
    #1

    I want a setting that allows users to import/export font setting and color from a setting file (text file). How do I convert them into string for storing? For QColor I think I can convert it to hex but I have no idea for QTextCharFormat ,

    J 1 Reply Last reply 17 Jul 2020, 04:32
    0
    • L lansing
      17 Jul 2020, 00:41

      I want a setting that allows users to import/export font setting and color from a setting file (text file). How do I convert them into string for storing? For QColor I think I can convert it to hex but I have no idea for QTextCharFormat ,

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Jul 2020, 04:32 last edited by
      #2

      @lansing You can implement

      QTextStream &	operator<<(const QTextCharFormat&)
      QTextStream &	operator>>(QTextCharFormat&)
      

      and serialise/de-serialise what ever QTextCharFormat properties you want.
      See https://doc.qt.io/qt-5/qtextstream.html

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

      L 1 Reply Last reply 17 Jul 2020, 05:23
      2
      • J jsulm
        17 Jul 2020, 04:32

        @lansing You can implement

        QTextStream &	operator<<(const QTextCharFormat&)
        QTextStream &	operator>>(QTextCharFormat&)
        

        and serialise/de-serialise what ever QTextCharFormat properties you want.
        See https://doc.qt.io/qt-5/qtextstream.html

        L Offline
        L Offline
        lansing
        wrote on 17 Jul 2020, 05:23 last edited by
        #3

        @jsulm

        Can you give an example on how to read with that syntax? I'm getting the error overloaded 'operator>>' must be a binary operator .

        J 1 Reply Last reply 17 Jul 2020, 05:43
        0
        • L lansing
          17 Jul 2020, 05:23

          @jsulm

          Can you give an example on how to read with that syntax? I'm getting the error overloaded 'operator>>' must be a binary operator .

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 17 Jul 2020, 05:43 last edited by
          #4

          @lansing There are examples in the link I posted.

          // I assume stream is a QTextStream here
          QTextCharFormat charFormat;
          stream >> charFormat;
          

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

          L 1 Reply Last reply 17 Jul 2020, 08:32
          0
          • J jsulm
            17 Jul 2020, 05:43

            @lansing There are examples in the link I posted.

            // I assume stream is a QTextStream here
            QTextCharFormat charFormat;
            stream >> charFormat;
            
            L Offline
            L Offline
            lansing
            wrote on 17 Jul 2020, 08:32 last edited by lansing
            #5

            @jsulm

            This is my test to write to a text file:

                QTextCharFormat  textCharFormat; // this has value inside
            
                QFile data("output.txt");
                if (data.open(QFile::WriteOnly | QFile::Truncate)) {
                    QTextStream out(&data);            
                    out << textCharFormat;
                }
            

            I got an error invalid operands to binary expression ('QTextStream' to 'QTextCharFormat').

            J 1 Reply Last reply 17 Jul 2020, 08:35
            0
            • L lansing
              17 Jul 2020, 08:32

              @jsulm

              This is my test to write to a text file:

                  QTextCharFormat  textCharFormat; // this has value inside
              
                  QFile data("output.txt");
                  if (data.open(QFile::WriteOnly | QFile::Truncate)) {
                      QTextStream out(&data);            
                      out << textCharFormat;
                  }
              

              I got an error invalid operands to binary expression ('QTextStream' to 'QTextCharFormat').

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 17 Jul 2020, 08:35 last edited by
              #6

              @lansing That's why I wrote before:
              You can implement

              QTextStream & operator<<(const QTextCharFormat&)
              QTextStream & operator>>(QTextCharFormat&)

              You have to implement those operators first.

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

              L 1 Reply Last reply 17 Jul 2020, 17:43
              1
              • J jsulm
                17 Jul 2020, 08:35

                @lansing That's why I wrote before:
                You can implement

                QTextStream & operator<<(const QTextCharFormat&)
                QTextStream & operator>>(QTextCharFormat&)

                You have to implement those operators first.

                L Offline
                L Offline
                lansing
                wrote on 17 Jul 2020, 17:43 last edited by
                #7

                @jsulm said in Storing QTextCharFormat and QColor to text file?:

                QTextStream & operator<<(const QTextCharFormat&)

                Hi, I have searched around how to write overloaded function for the operator, but I have not seen any example on the syntax you're using.

                I have declared QTextStream & operator<<(const QTextCharFormat&) in the class' header file, and in the source file, it should be something like this?

                QTextStream &SettingsDialog::operator<<(const QTextCharFormat &b)
                {
                    stream << "font: " << b.font().toString() << " font-family: " << b.fontFamily();
                    return stream;
                }
                

                But I don't know where do I pass in the stream in the implementation.

                1 Reply Last reply
                0

                3/7

                17 Jul 2020, 05:23

                • Login

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