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?
QtWS25 Last Chance

Storing QTextCharFormat and QColor to text file?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtextcharformatqcolorconvert
7 Posts 2 Posters 1.1k 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.
  • L Offline
    L Offline
    lansing
    wrote on 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 ,

    jsulmJ 1 Reply Last reply
    0
    • L lansing

      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 ,

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      2
      • jsulmJ jsulm

        @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 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 .

        jsulmJ 1 Reply Last reply
        0
        • L lansing

          @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 .

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          0
          • jsulmJ jsulm

            @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 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').

            jsulmJ 1 Reply Last reply
            0
            • L lansing

              @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').

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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
              1
              • jsulmJ jsulm

                @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 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

                • Login

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