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. Why does select(QTextCursor::BlockUnderCursor) include an extra junk character?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.3k 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.
  • T Offline
    T Offline
    Therefore
    wrote on last edited by
    #1

    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:

    @A
    B@

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

    @#include <QTGui>
    int main(int argc, char argv[])
    {
    QApplication app(argc, argv);
    QMainWindow
    window = new QMainWindow;
    QTextEdit* editor = new QTextEdit(window);
    QTextDocument* document = new QTextDocument(window);

    editor->setDocument(document);
    QFile file&#40;"test.txt"&#41;;
    if (file.open(QFile::ReadOnly | QFile::Text))
        editor->setPlainText(file.readAll());
    
    QTextBlock block = document->begin();
    QTextCursor* cursor = new QTextCursor(document);
    int pos = cursor->position();           // = 0
    int anchor = cursor->anchor();          // = 0
    
    cursor->select(QTextCursor::BlockUnderCursor);
    pos = cursor->position();               // = 1
    anchor = cursor->anchor();              // = 0
    
    QString text = cursor->selectedText();  // = "A"
    int size = text.size();                 // = 1
    
    cursor->movePosition(QTextCursor::NextBlock);
    pos = cursor->position();               // = 2
    anchor = cursor->anchor();              // = 2
    
    cursor->select(QTextCursor::BlockUnderCursor);
    pos = cursor->position();               // = 3
    anchor = cursor->anchor();              // = 1 Why not 2?
    
    text = cursor->selectedText();          // "B" in debugger
                                            // but text.at(0) = junk & test.at(1) = "B"
    size = text.size();                     // = 2 Why? Why not 1?
    
    return app.exec();
    

    }@

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tzander
      wrote on last edited by
      #2

      Thats the end-of-block character you find.

      There are also end-of-table-cell characters.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Therefore
        wrote on last edited by
        #3

        I see. That makes sense now. Thanks!

        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