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. [SOLVED] How to get the last sentence from QTextEdit?

[SOLVED] How to get the last sentence from QTextEdit?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 6.1k 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.
  • S Offline
    S Offline
    scroverty
    wrote on last edited by
    #1

    Hi, I'm trying to get the last sentence from QTextEdit after the user type a string contain '.' || '?' || '!' char.

    How to implement it the shortess way? Currently, I'm using QStringList and QString with QRegExp for this, but it doesn't work as wanted.

    Alvis Ar'Berkeley Andrew.
    Pleased to meet you!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vezprog
      wrote on last edited by
      #2

      Are you running in debug mode using the creator? you can probably pin point where the error is coming from. Or simply just step through the code line by line and see where it crashes.

      Stepping through it can make a world of difference.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        scroverty
        wrote on last edited by
        #3

        After some fixed, I managed to run the program and typing string with '.' char without crash. But it won't get me the last sentence, which should be the string list from the last '.' char to the newest '.' char.

        It seem like the argument:

        @..
        if (! sentencesString.lastIndexOf(".") == -1)
        ..@

        will always false, and it will do the else statement.

        So it seem my algorithm is wrong somehow?...

        Edit: Seem like problem with QRegExp. the "." matches anystring, then what should I use to find the last "." ?

        Alvis Ar'Berkeley Andrew.
        Pleased to meet you!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          scroverty
          wrote on last edited by
          #4

          Updated the question.

          I dont know how to use QtRegExp much, and now, I realized that I need to have '?' and '!' too.

          If someone can support me with some example code then, many thanks!

          Alvis Ar'Berkeley Andrew.
          Pleased to meet you!

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            The above code is wrong IYAM. The compiler will probably read it as follows:

            • Execute the lastIndexOf function
            • Get the return value, and make it negative (the ! before the function)
            • Check if the negative return value equals - 1
              Think that will cause the always false result. Should do it like this:
              @if (sentences.lastIndexOf(".") != -1)@
              Can't test the code. No Qt on this PC.
              I didn't use the QtRegExp much, so any help from me might be a bit off. Hopefully someone else can help in that area.
              greetz

            Greetz, Jeroen

            1 Reply Last reply
            0
            • S Offline
              S Offline
              scroverty
              wrote on last edited by
              #6

              @greetz: Thanks! I will try it!

              Alvis Ar'Berkeley Andrew.
              Pleased to meet you!

              1 Reply Last reply
              0
              • JeroentjehomeJ Offline
                JeroentjehomeJ Offline
                Jeroentjehome
                wrote on last edited by
                #7

                Your welcome. Be carefull with using the ! in a if function with a comparison inside.
                What you normally see is something like:
                @if (!sentences.contains ("."))@
                The contains returns a bool, so a false as return value will result in a true for the if().

                Greetz, Jeroen

                1 Reply Last reply
                0
                • JeroentjehomeJ Offline
                  JeroentjehomeJ Offline
                  Jeroentjehome
                  wrote on last edited by
                  #8

                  Maybe to understand it better, place this code beneath each other and compare what happens with the debugger:
                  @
                  int iNormal = sentences.lastIndexOf(".");
                  int iInverted = !sentences.lastIndexOf(".");
                  @
                  The iInverted was the value you compared to -1, so only with luck it will result in true.
                  Hope this helps.

                  Greetz, Jeroen

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    scroverty
                    wrote on last edited by
                    #9

                    Thanks for your information!

                    But it seem the funtion I wrote itself is failure, since the string out is not the last sentence, but the whole textEditor's text.

                    Here are my codes, it runs, but not like what I wanted:

                    @ ...
                    QString currentTextOnBoard = ui->textEditor->toPlainText ();

                    QStringList currentSentence = currentTextOnBoard.split (QRegExp("\b"));

                     if (!currentSentence.isEmpty ())
                    
                         if (currentSentence.endsWith (".")){
                    
                             currentSentence.removeLast ();
                    
                             if (currentSentence.lastIndexOf (QRegExp("*. ?\s$"))!=-1)
                    
                                 for (int i = currentSentence.lastIndexOf (QRegExp("*. ?\s$")); i < currentSentence.size (); ++i)
                    
                                         logS<<currentSentence.at (i);
                    
                             else
                    
                                   for (int i = 0; i < currentSentence.size (); ++i)
                    
                                        logS<<currentSentence.at (i);
                    
                                     }@
                    

                    logS is QTextStream point to file out.

                    I think of implement the above funtion some other way (sorter/smarter).

                    Any suggestion? Thanks alot!

                    Alvis Ar'Berkeley Andrew.
                    Pleased to meet you!

                    1 Reply Last reply
                    0
                    • JeroentjehomeJ Offline
                      JeroentjehomeJ Offline
                      Jeroentjehome
                      wrote on last edited by
                      #10

                      hmm, will have to take a longer look later on.
                      Couple of questions, but why do you use WordBoundary at the split function? That will give every word in a single position of the qstringlist. Shouldn't you use a split(".");
                      Better yet, use the QString::count (".") to get the number of points in the qstring. Then get the last piece of that code with QString::section ('.', count - 1, count);
                      No Qt on this machine to test it, but this idea might work.

                      greetz

                      Greetz, Jeroen

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        scroverty
                        wrote on last edited by
                        #11

                        greetz: Your ideas sounds good! I will look into the class's usage and implement the code right away!

                        Thanks!

                        Alvis Ar'Berkeley Andrew.
                        Pleased to meet you!

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          scroverty
                          wrote on last edited by
                          #12

                          Thanks Alot greetz! It works just as wanted. Thank you so much!!! :D

                          Here are my codes for those who interested:

                          @
                          if (currentTextOnBoard.endsWith ('.') ){

                                       int count = 0;
                                       count +=  currentTextOnBoard.count ('.');
                          
                                       currentSentence = currentTextOnBoard.section ('.', count-1, count);
                          
                                       logS<<currentSentence<<"\n";
                          

                          @

                          Agains, thank you greetz! Smart and Short :D

                          Alvis Ar'Berkeley Andrew.
                          Pleased to meet you!

                          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