I am a bit confused about how the `QTextBlock::iterator` works:

The documentation shows clear examples of how to use it, on normal text:

```
QTextBlock::iterator it;
for (it = currentBlock.begin(); !(it.atEnd()); ++it) {
    QTextFragment currentFragment = it.fragment();
    if (currentFragment.isValid())
        processFragment(currentFragment);
}

```

I encounter problems on empty lines of text. On those lines,

```
it = currentBlock.begin();
if(it.atEnd())
    // returns true !

```

I still need to be able to read formatting (char and block) 

Should I check the block at end ? Is there any other way to test blocks with nothing except the new line ?

My current solution: check the last iterator as well, separate from the "for" loop, and also test if it is the last block in the document (if I try to get the fragment of the last block in the document, the program crashes).

It seems that I am working against the documentation... How should I get the formatting of empty lines ?

Edit:

My old solution:

```
QTextBlock currentBlock = document()->findBlock(selStart);
QTextBlock lastBlock = document()->lastBlock();
while (currentBlock.isValid())
{
    QTextBlock::iterator it = currentBlock.begin();
    if(currentBlock != lastBlock && it.atEnd())
    {
        QTextFragment currentFragment = it.fragment();
        if (currentFragment.isValid())
        {
            QTextCharFormat f = currentFragment.charFormat();
            // do something
        }
    } 
    else
    {
        for (; !(it.atEnd()); ++it)
        {
            QTextFragment currentFragment = it.fragment();
            if (currentFragment.isValid())
            {
                // do stuff
                QTextCharFormat f = currentFragment.charFormat();
                // do stuff
            }
        }
    }
}

```

New solution based from answer from Tarod eliminates one test (but seems to have less consistent behavior)

```
QTextBlock currentBlock = document()->findBlock(selStart);
QTextBlock lastBlock = document()->lastBlock();
while (currentBlock.isValid())
{
    QTextBlock::iterator it = currentBlock.begin();
    if(currentBlock != lastBlock && it.atEnd())
    {
        QTextCharFormat f = currentBlock.charFormat();
        // do something
    } 
    else
    {
        for (; !(it.atEnd()); ++it)
        {
            QTextFragment currentFragment = it.fragment();
            if (currentFragment.isValid())
            {
                // do stuff
                QTextCharFormat f = currentFragment.charFormat();
                // do stuff
            }
        }
    }
}

```

I still need to check against last block and avoid using it if empty, sometimes it crashes.