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 last edited by HenkCoder
    #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?

    artwawA 1 Reply Last reply
    0
    • H HenkCoder

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      • H HenkCoder

        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?

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on 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
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          0
          • Christian EhrlicherC Christian Ehrlicher

            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 last edited by
            #4

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

            1 Reply Last reply
            0
            • artwawA artwaw

              @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 last edited by
              #5

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

              artwawA 1 Reply Last reply
              0
              • H HenkCoder

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

                artwawA Offline
                artwawA Offline
                artwaw
                wrote on 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
                • artwawA artwaw

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

                  JonBJ 1 Reply Last reply
                  0
                  • H HenkCoder

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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
                    • JonBJ JonB

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

                      JonBJ 1 Reply Last reply
                      0
                      • H HenkCoder

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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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
                        0
                        • JonBJ JonB

                          @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 last edited by
                          #11

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

                          H 1 Reply Last reply
                          0
                          • H HenkCoder

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

                            H Offline
                            H Offline
                            HenkCoder
                            wrote on last edited by
                            #12

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

                            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