Results 1 to 4 of 4

Thread: Why does select(QTextCursor::BlockUnderCursor) include an extra junk character?

  1. #1
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    3
    Qt products
    Qt4

    Default Why does select(QTextCursor::BlockUnderCursor) include an extra junk character?

    Windows 7 SP1
    MSVS 2010
    Qt 4.8.4

    I am using QTextCursor to grab each block's text. I use select(QTextCursor::BlockUnderCursor) to grab the text and then go to the next block with movePosition(QTextCursor::NextBlock). But when I again select(QTextCursor::BlockUnderCursor) I get an extra junk character in the QString and the anchor has moved to the end of the previous block.

    Using this for text.txt:

    Qt Code:
    1. A
    2. B
    To copy to clipboard, switch view to plain text mode 

    This code's comments walks through the issue and asks the questions:

    Qt Code:
    1. #include <QTGui>
    2. int main(int argc, char *argv[])
    3. {
    4. QApplication app(argc, argv);
    5. QMainWindow* window = new QMainWindow;
    6. QTextEdit* editor = new QTextEdit(window);
    7. QTextDocument* document = new QTextDocument(window);
    8.  
    9. editor->setDocument(document);
    10. QFile file("test.txt");
    11. if (file.open(QFile::ReadOnly | QFile::Text))
    12. editor->setPlainText(file.readAll());
    13.  
    14. QTextBlock block = document->begin();
    15. QTextCursor* cursor = new QTextCursor(document);
    16. int pos = cursor->position(); // = 0
    17. int anchor = cursor->anchor(); // = 0
    18.  
    19. cursor->select(QTextCursor::BlockUnderCursor);
    20. pos = cursor->position(); // = 1
    21. anchor = cursor->anchor(); // = 0
    22.  
    23. QString text = cursor->selectedText(); // = "A"
    24. int size = text.size(); // = 1
    25.  
    26. cursor->movePosition(QTextCursor::NextBlock);
    27. pos = cursor->position(); // = 2
    28. anchor = cursor->anchor(); // = 2
    29.  
    30. cursor->select(QTextCursor::BlockUnderCursor);
    31. pos = cursor->position(); // = 3
    32. anchor = cursor->anchor(); // = 1 Why not 2?
    33.  
    34. text = cursor->selectedText(); // "B" in debugger
    35. // but text.at(0) = junk & test.at(1) = "B"
    36. size = text.size(); // = 2 Why? Why not 1?
    37.  
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by therefore; 13th February 2013 at 22:45.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Why does select(QTextCursor::BlockUnderCursor) include an extra junk character?

    From the docs ( QTextCursor::selectedText() ):
    Note: If the selection obtained from an editor spans a line break, the text will contain a Unicode U+2029 paragraph separator character instead of a newline \n character.
    You can check by putting the following statement in your code:
    Qt Code:
    1. qDebug() << QString("U+%1").arg(text.at(0).unicode(),0,16);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to norobro for this useful post:

    therefore (14th February 2013)

  4. #3
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    3
    Qt products
    Qt4

    Default Re: Why does select(QTextCursor::BlockUnderCursor) include an extra junk character?

    Thanks for the education! Now I get it. In other words, selecting the block includes the starting paragraph separator. The first block doesn't have a starting SEP. Therefore, need to exclude the first character if one wants to extract the text alone of subsequent blocks.

  5. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Why does select(QTextCursor::BlockUnderCursor) include an extra junk character?

    You're welcome. If you just want the text use QString::trimmed():
    Qt Code:
    1. text = cursor->selectedText().trimmed();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 7
    Last Post: 11th November 2014, 08:40
  2. Character by Character (Unicode?) File Reading
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2009, 15:28
  3. QTextCursor select part of a word?
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 13th February 2008, 17:01
  4. QGraphicsItem leaves junk on screen
    By Gopala Krishna in forum Qt Programming
    Replies: 18
    Last Post: 12th December 2007, 06:23
  5. QSqlQuery always return junk value while for varchar oracle.
    By ranjit2709 in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2007, 05:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.