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. Don't save files with changed font and color
Forum Updated to NodeBB v4.3 + New Features

Don't save files with changed font and color

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 701 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.
  • M Offline
    M Offline
    MorOver
    wrote on last edited by MorOver
    #1

    Hi, I am creating an Notepad and I have problem with my color and font because when I changed color I can see it on textctrl but after saving and opening file I got only text ...

    QColor color = QColorDialog::getColor(Qt::white,this,"Choose color");
         if(color.isValid()) {
    
             ui->textEdit->setTextColor(color);
         }
    
    JonBJ 1 Reply Last reply
    0
    • M MorOver

      Hi, I am creating an Notepad and I have problem with my color and font because when I changed color I can see it on textctrl but after saving and opening file I got only text ...

      QColor color = QColorDialog::getColor(Qt::white,this,"Choose color");
           if(color.isValid()) {
      
               ui->textEdit->setTextColor(color);
           }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @MorOver
      Welcome to the forum!

      How do you save what to this "Notepad file"? What do you do about saving the color, and/or what you want this color applied to?

      M 1 Reply Last reply
      1
      • JonBJ JonB

        @MorOver
        Welcome to the forum!

        How do you save what to this "Notepad file"? What do you do about saving the color, and/or what you want this color applied to?

        M Offline
        M Offline
        MorOver
        wrote on last edited by MorOver
        #3

        @JonB Hello :)
        This is my save slot

        QString file = QFileDialog::getSaveFileName(this, "Save", QDir::homePath(),"Text file (*.txt);;C files (*.c);;C++ files (*.cpp);;Docx files (*.doc)");
            QFile File(file);
            if(!Plik.open(QIODevice::WriteOnly))
            {
                qDebug() << "error opening file: " << Plik.error();
                return;
            }
            QString text;
            QTextStream out(&file);
            text =  ui->textEdit->toPlainText();
            out << text;
            file.flush();
                file.close();
        

        I just created simple Notepad app like open/save file and I want add color text and font size slots, one of these is above but where I saved my file to *.txt or *.doc extensions and open I see only text without any changes(I mean without font and color).

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

          @MorOver said in Don't save files with changed font and color:

          ui->textEdit->toPlainText();

          You convert it to a plain text without any formatting information - how do you expect that a color or any other format can be restored afterwards? Take a look at toHtml() for example.

          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
          4
          • M MorOver

            @JonB Hello :)
            This is my save slot

            QString file = QFileDialog::getSaveFileName(this, "Save", QDir::homePath(),"Text file (*.txt);;C files (*.c);;C++ files (*.cpp);;Docx files (*.doc)");
                QFile File(file);
                if(!Plik.open(QIODevice::WriteOnly))
                {
                    qDebug() << "error opening file: " << Plik.error();
                    return;
                }
                QString text;
                QTextStream out(&file);
                text =  ui->textEdit->toPlainText();
                out << text;
                file.flush();
                    file.close();
            

            I just created simple Notepad app like open/save file and I want add color text and font size slots, one of these is above but where I saved my file to *.txt or *.doc extensions and open I see only text without any changes(I mean without font and color).

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @MorOver
            As @Christian-Ehrlicher has said.

            But you have a conceptual problem. Your proposed file types:

            Text file (*.txt);;C files (*.c);;C++ files (*.cpp);;Docx files (*.doc)

            First of all you should scrub .doc, you're not making any kind of Word doc file. But the rest are "plain text". And that would mean they can't be saved as HTML, and can't contain any saved color information. So you're going to have to think about what you actually want!

            1 Reply Last reply
            1
            • M Offline
              M Offline
              MorOver
              wrote on last edited by
              #6

              @JonB So which extension file could save text with color and font?

              JonBJ 1 Reply Last reply
              0
              • M MorOver

                @JonB So which extension file could save text with color and font?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @MorOver
                It's not the extension per se, it's whether you want to be able to use it as a text file or not.

                For example, you just cannot put something to indicate a color or a font into a .cpp file which is to be used as C++ source for compiling.

                If you save from QTextEdit::toHtml() the only really correct thing is to save it as a .htm/.html file.

                If you look at, say, Microsoft Windows WordPad, which is an editor like yours in that you can save formatting --- and you cannot use it as a text or C++ file --- you'll see it saves as a .doc or a .rtf file, and the content is indeed RTF, which is like HTML.

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  MorOver
                  wrote on last edited by
                  #8

                  @JonB Thank you for help ,It works when I changed method from toPlainText() to toHtml() and saved file in *.doc extension, I can open formatted text in my WordPad.

                  JonBJ 1 Reply Last reply
                  0
                  • M MorOver

                    @JonB Thank you for help ,It works when I changed method from toPlainText() to toHtml() and saved file in *.doc extension, I can open formatted text in my WordPad.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @MorOver
                    Good.

                    But the .doc extension is a bit dubious. It's true that you can just save HTML to a .doc and, say, WordPad or Word will be able to read it in OK. But the reverse is not necessarily true: save a .doc from WordPad/Word and then try to read it back into your QTextEdit. It should not be good! Because QTextEdit can't begin to cope with what can be in a .doc.

                    So be aware that if your user thinks they can read any .doc back into your editor they will be disappointed. They can only really read .doc files you have created from your app. That's why .htm would be a more accurate extension. Up to you.

                    1 Reply Last reply
                    3

                    • Login

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