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. Save a .rtf file formatting the text in a QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Save a .rtf file formatting the text in a QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 1.4k 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.
  • H Offline
    H Offline
    HenkCoder
    wrote on 1 Aug 2021, 12:28 last edited by HenkCoder 8 Jan 2021, 12:34
    #1

    I'm currently making a notepad and I've found a problem. I want to save a file formatting the text so with different fonts, point sizes and colors in a .rtf file, but it just saves without formatting it so without any color or different fonts.

    Here is the code I put to save a file:

    void MainWindow::on_save_clicked()
    {
        QTextEdit *edit = getTabTextEdit();
        QString fileName;
        if (currentFile.isEmpty())
        {
            fileName = QFileDialog::getSaveFileName(this, tr("Save a file"), "New Document", tr("Text File (*.txt);; RichTextFormat File (*.rtf);; All Files (*)"));
            currentFile = fileName;
        }
        else
        {
            fileName = currentFile;
        }
        QFile file(fileName);
        if(ui->tabWidget->currentWidget() == ui->tab_1)
        {
            if(file.open(QIODevice::WriteOnly | QFile::Text))
            {
                ui->tabWidget->setTabText(ui->tabWidget->currentIndex(), file.fileName());
                QTextStream out (&file);
                out << ui->edit->toPlainText();
                file.close();
                ui->edit->setFocus();
            }
        }
        else
        {
            if(file.open(QIODevice::WriteOnly | QFile::Text))
            {
                ui->tabWidget->setTabText(ui->tabWidget->currentIndex(), file.fileName());
                QTextStream out (&file);
                out << edit->toPlainText();
                file.close();
                edit->setFocus();
            }
        }
    }
    

    Is there a way to save a file formatting the text of the QTextEdit?

    A 1 Reply Last reply 1 Aug 2021, 12:31
    0
    • H HenkCoder
      1 Aug 2021, 13:22

      @artwaw
      Oh, I really don't know how to do this, I don't know much about Qt, can someone give me some help?

      J Offline
      J Offline
      JonB
      wrote on 1 Aug 2021, 13:35 last edited by
      #8

      @HenkCoder
      Basically you can't, or it's hard. Why do you have to have .RTF format anyway? Use HTML (Qt's "rich text").

      H 1 Reply Last reply 1 Aug 2021, 17:04
      1
      • H HenkCoder
        1 Aug 2021, 12:28

        I'm currently making a notepad and I've found a problem. I want to save a file formatting the text so with different fonts, point sizes and colors in a .rtf file, but it just saves without formatting it so without any color or different fonts.

        Here is the code I put to save a file:

        void MainWindow::on_save_clicked()
        {
            QTextEdit *edit = getTabTextEdit();
            QString fileName;
            if (currentFile.isEmpty())
            {
                fileName = QFileDialog::getSaveFileName(this, tr("Save a file"), "New Document", tr("Text File (*.txt);; RichTextFormat File (*.rtf);; All Files (*)"));
                currentFile = fileName;
            }
            else
            {
                fileName = currentFile;
            }
            QFile file(fileName);
            if(ui->tabWidget->currentWidget() == ui->tab_1)
            {
                if(file.open(QIODevice::WriteOnly | QFile::Text))
                {
                    ui->tabWidget->setTabText(ui->tabWidget->currentIndex(), file.fileName());
                    QTextStream out (&file);
                    out << ui->edit->toPlainText();
                    file.close();
                    ui->edit->setFocus();
                }
            }
            else
            {
                if(file.open(QIODevice::WriteOnly | QFile::Text))
                {
                    ui->tabWidget->setTabText(ui->tabWidget->currentIndex(), file.fileName());
                    QTextStream out (&file);
                    out << edit->toPlainText();
                    file.close();
                    edit->setFocus();
                }
            }
        }
        

        Is there a way to save a file formatting the text of the QTextEdit?

        A Offline
        A Offline
        artwaw
        wrote on 1 Aug 2021, 12:31 last edited by
        #2

        @HenkCoder There is, to some extent. Please see overview under the link below.

        https://doc.qt.io/qt-5/richtext.html

        For more information please re-read.

        Kind Regards,
        Artur

        H 1 Reply Last reply 1 Aug 2021, 12:56
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 1 Aug 2021, 12:38 last edited by
          #3

          As already told on so you use toPlaintText() and wonder why the formatting is not saved... isn't the function name meaningful enough?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          H 1 Reply Last reply 1 Aug 2021, 12:39
          0
          • C Christian Ehrlicher
            1 Aug 2021, 12:38

            As already told on so you use toPlaintText() and wonder why the formatting is not saved... isn't the function name meaningful enough?

            H Offline
            H Offline
            HenkCoder
            wrote on 1 Aug 2021, 12:39 last edited by
            #4

            @Christian-Ehrlicher alright, but is there a way to get the formatted text?

            1 Reply Last reply
            0
            • A artwaw
              1 Aug 2021, 12:31

              @HenkCoder There is, to some extent. Please see overview under the link below.

              https://doc.qt.io/qt-5/richtext.html

              H Offline
              H Offline
              HenkCoder
              wrote on 1 Aug 2021, 12:56 last edited by
              #5

              @artwaw Hey, I read the documentation and I wasn't able to find anything useful to my problem.

              A 1 Reply Last reply 1 Aug 2021, 13:03
              0
              • H HenkCoder
                1 Aug 2021, 12:56

                @artwaw Hey, I read the documentation and I wasn't able to find anything useful to my problem.

                A Offline
                A Offline
                artwaw
                wrote on 1 Aug 2021, 13:03 last edited by
                #6

                @HenkCoder I suggest you read that again maybe?

                As documentation states, rich text formatting in Qt is done via HTML subset. So, in case of QTextEdit, you will look at toHtml() method to obtain formatted text.

                RTF is closed format, owned by Microsoft, however its specification is available - since Qt lacks direct support my approach would be to implement a model that converts Qt Rich Text to RTF and vice versa https://docs.microsoft.com/en-us/previous-versions/office/developer/office2000/aa140280(v=office.10)?redirectedfrom=MSDN

                For more information please re-read.

                Kind Regards,
                Artur

                H 1 Reply Last reply 1 Aug 2021, 13:22
                1
                • A artwaw
                  1 Aug 2021, 13:03

                  @HenkCoder I suggest you read that again maybe?

                  As documentation states, rich text formatting in Qt is done via HTML subset. So, in case of QTextEdit, you will look at toHtml() method to obtain formatted text.

                  RTF is closed format, owned by Microsoft, however its specification is available - since Qt lacks direct support my approach would be to implement a model that converts Qt Rich Text to RTF and vice versa https://docs.microsoft.com/en-us/previous-versions/office/developer/office2000/aa140280(v=office.10)?redirectedfrom=MSDN

                  H Offline
                  H Offline
                  HenkCoder
                  wrote on 1 Aug 2021, 13:22 last edited by
                  #7

                  @artwaw
                  Oh, I really don't know how to do this, I don't know much about Qt, can someone give me some help?

                  J 1 Reply Last reply 1 Aug 2021, 13:35
                  0
                  • H HenkCoder
                    1 Aug 2021, 13:22

                    @artwaw
                    Oh, I really don't know how to do this, I don't know much about Qt, can someone give me some help?

                    J Offline
                    J Offline
                    JonB
                    wrote on 1 Aug 2021, 13:35 last edited by
                    #8

                    @HenkCoder
                    Basically you can't, or it's hard. Why do you have to have .RTF format anyway? Use HTML (Qt's "rich text").

                    H 1 Reply Last reply 1 Aug 2021, 17:04
                    1
                    • J JonB
                      1 Aug 2021, 13:35

                      @HenkCoder
                      Basically you can't, or it's hard. Why do you have to have .RTF format anyway? Use HTML (Qt's "rich text").

                      H Offline
                      H Offline
                      HenkCoder
                      wrote on 1 Aug 2021, 17:04 last edited by
                      #9

                      @JonB because I want to save a file, I can't let the user save an HTML file, he may not understand it

                      J 1 Reply Last reply 1 Aug 2021, 17:07
                      0
                      • H HenkCoder
                        1 Aug 2021, 17:04

                        @JonB because I want to save a file, I can't let the user save an HTML file, he may not understand it

                        J Offline
                        J Offline
                        JonB
                        wrote on 1 Aug 2021, 17:07 last edited by
                        #10

                        @HenkCoder
                        RTF is a Microsoft format, effectively. " he may not understand it" --- "he" who/what? It's an "unusual" format to want to save in. Anyway, the answer remains the same.

                        H 1 Reply Last reply 1 Aug 2021, 17:20
                        0
                        • J JonB
                          1 Aug 2021, 17:07

                          @HenkCoder
                          RTF is a Microsoft format, effectively. " he may not understand it" --- "he" who/what? It's an "unusual" format to want to save in. Anyway, the answer remains the same.

                          H Offline
                          H Offline
                          HenkCoder
                          wrote on 1 Aug 2021, 17:20 last edited by
                          #11

                          @JonB with "he" i mean the user.
                          So I can't save with text formatting?

                          H 1 Reply Last reply 1 Aug 2021, 17:30
                          0
                          • H HenkCoder
                            1 Aug 2021, 17:20

                            @JonB with "he" i mean the user.
                            So I can't save with text formatting?

                            H Offline
                            H Offline
                            HenkCoder
                            wrote on 1 Aug 2021, 17:30 last edited by
                            #12

                            @HenkCoder oh nevermind, I managed to understand. Thank you for your help.

                            1 Reply Last reply
                            0

                            1/12

                            1 Aug 2021, 12:28

                            • Login

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