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. Text processing in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Text processing in QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.0k Views 2 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.
  • T Offline
    T Offline
    Troyer
    wrote on last edited by
    #1

    Hi!
    I Made it possible to take text from a file and encountered a problem:
    I need to find and output in reverse alphabetical order the vowel letters from each line.
    Most likely, i need to solve this through an array of characters to take from the string. But how to implement it?

    void MainWindow::on_pushButton_clicked()
    {
        QFile file("F:/Labs/Text/Text.txt");
            if(file.open(QIODevice::ReadOnly))
            {
                QTextStream stream(&file);
                QString str = stream.readAll();
                ui->textEdit->setText(str);
                file.close();
            }
    
    }
    
    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      I don't understand your need.
      But if you want to read the file line by line, you could use

      while(!stream.atEnd()) {
          str = stream.readLine();
          ui->textEdit->append(str);
      }
      
      T 1 Reply Last reply
      1
      • B Bonnie

        I don't understand your need.
        But if you want to read the file line by line, you could use

        while(!stream.atEnd()) {
            str = stream.readLine();
            ui->textEdit->append(str);
        }
        
        T Offline
        T Offline
        Troyer
        wrote on last edited by
        #3

        @Bonnie I took the text from a file and put it in QTextEdit, and now I need to somehow output in reverse alphabetical order the vowel letters from each line of this text

        B 1 Reply Last reply
        0
        • T Troyer

          @Bonnie I took the text from a file and put it in QTextEdit, and now I need to somehow output in reverse alphabetical order the vowel letters from each line of this text

          B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          @Troyer
          Sorry I don't understand that sentence. Maybe my English is not good enough. :(
          Do you have any example of the input and output text?

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            If i understood correctly then something like this.

            void Test()
            {
            
                QString line("the hens ate the wolves under the brigde");
                QList<QChar> vowels;
                for (int i = 0; i < line.length(); ++i) {
                    QCharRef letter(line[i]);
                    if (    letter == 'a' || letter == 'e' || letter == 'i' ||
                            letter == 'o' || letter == 'u' || letter == 'A' ||
                            letter == 'E' || letter == 'I' || letter == 'O' ||
                            letter == 'U') {
                        vowels.push_back(letter);
                    }
                }
            
                std::sort( vowels.begin(), vowels.end(), [](QChar & p1, QChar & p2) {
                    return p1 > p2;
                } );
            
                for (QChar &item : vowels) {
                    qDebug() << "=" << item;
                }
            }
            
            

            = 'u'
            = 'o'
            = 'i'
            = 'e'
            = 'e'
            = 'e'
            = 'e'
            = 'e'
            = 'e'
            = 'e'
            = 'e'
            = 'a'

            T 1 Reply Last reply
            2
            • mrjjM mrjj

              Hi
              If i understood correctly then something like this.

              void Test()
              {
              
                  QString line("the hens ate the wolves under the brigde");
                  QList<QChar> vowels;
                  for (int i = 0; i < line.length(); ++i) {
                      QCharRef letter(line[i]);
                      if (    letter == 'a' || letter == 'e' || letter == 'i' ||
                              letter == 'o' || letter == 'u' || letter == 'A' ||
                              letter == 'E' || letter == 'I' || letter == 'O' ||
                              letter == 'U') {
                          vowels.push_back(letter);
                      }
                  }
              
                  std::sort( vowels.begin(), vowels.end(), [](QChar & p1, QChar & p2) {
                      return p1 > p2;
                  } );
              
                  for (QChar &item : vowels) {
                      qDebug() << "=" << item;
                  }
              }
              
              

              = 'u'
              = 'o'
              = 'i'
              = 'e'
              = 'e'
              = 'e'
              = 'e'
              = 'e'
              = 'e'
              = 'e'
              = 'e'
              = 'a'

              T Offline
              T Offline
              Troyer
              wrote on last edited by Troyer
              #6

              @mrjj
              Yes. Thanks a million!

              mrjjM 1 Reply Last reply
              0
              • T Troyer

                @mrjj
                Yes. Thanks a million!

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Troyer
                Np, do note that is not the most pretty way. the vowels could be in a list and so on and we use
                QChar to store it which might be overkill if only ascii but for a smaller text file it wont really matter.

                T 1 Reply Last reply
                1
                • mrjjM mrjj

                  @Troyer
                  Np, do note that is not the most pretty way. the vowels could be in a list and so on and we use
                  QChar to store it which might be overkill if only ascii but for a smaller text file it wont really matter.

                  T Offline
                  T Offline
                  Troyer
                  wrote on last edited by Troyer
                  #8

                  @mrjj Hello again!
                  I still find it difficult to analyze your code, I'm not professional enough yet :)
                  And I would like to ask you one more question. I need to output all characters in QTextEdit, not in the app console (you use QDebug for this)
                  I tried converting all the characters to a string and then just output the text: ui - >textEdit - >setText(str), but failed. What should I do now?
                  (Sorry for my English, not my main one)

                  jsulmJ 1 Reply Last reply
                  0
                  • T Troyer

                    @mrjj Hello again!
                    I still find it difficult to analyze your code, I'm not professional enough yet :)
                    And I would like to ask you one more question. I need to output all characters in QTextEdit, not in the app console (you use QDebug for this)
                    I tried converting all the characters to a string and then just output the text: ui - >textEdit - >setText(str), but failed. What should I do now?
                    (Sorry for my English, not my main one)

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Troyer said in Text processing in QTextEdit:

                    but failed

                    In what way failed? What happened? Can you show the code?

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

                    T 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @Troyer said in Text processing in QTextEdit:

                      but failed

                      In what way failed? What happened? Can you show the code?

                      T Offline
                      T Offline
                      Troyer
                      wrote on last edited by
                      #10

                      @jsulm Already solved the problem)

                      Pablo J. RoginaP 1 Reply Last reply
                      0
                      • T Troyer

                        @jsulm Already solved the problem)

                        Pablo J. RoginaP Offline
                        Pablo J. RoginaP Offline
                        Pablo J. Rogina
                        wrote on last edited by
                        #11

                        @Troyer said in Text processing in QTextEdit:

                        Already solved the problem)

                        would you mind sharing the solution?

                        in addition, please don't forget to mark your post as solved!

                        Upvote the answer(s) that helped you solve the issue
                        Use "Topic Tools" button to mark your post as Solved
                        Add screenshots via postimage.org
                        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                        T 1 Reply Last reply
                        0
                        • Pablo J. RoginaP Pablo J. Rogina

                          @Troyer said in Text processing in QTextEdit:

                          Already solved the problem)

                          would you mind sharing the solution?

                          in addition, please don't forget to mark your post as solved!

                          T Offline
                          T Offline
                          Troyer
                          wrote on last edited by
                          #12

                          @Pablo-J-Rogina
                          Yes, of course. All i had to do was add "append" :)

                                          for (QChar &item : vowels) {
                                              ui->textEdit_2->append(item);
                                          }
                          
                          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