Important: Please read the Qt Code of Conduct -

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

  • 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.

  • 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.

  • 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 "." ?

  • 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!

  • 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: Thanks! I will try it!

  • 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().

  • 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.

  • 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<< (i);
                   for (int i = 0; i < currentSentence.size (); ++i)
                        logS<< (i);

    logS is QTextStream point to file out.

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

    Any suggestion? Thanks alot!

  • 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: Your ideas sounds good! I will look into the class's usage and implement the code right away!


  • 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);


    Agains, thank you greetz! Smart and Short :D

Log in to reply