I want to map two pointers in a QMap Object to store their relation. The key pointer points to a QTextBlock while the value pointer points to a widget. The aim is to update the position of the widget in relation to the position of the QTextBlock in the QPlainTextEdit when something in the QPlainTextEdit changes. The widget should get destroyed when the QTextBlock gets destroyed.
However, I'm not familiar with the behaviour of a QTextBlock in QPlainTextEdit. Though the firstVisibleBlock() method does not seem to return a pointer to a QTextBlock in the QPlainTextEdit I create a new QTextBlock with the QTextBlock from the QPlainTextEdit as parameter.
QTextBlock* CodeEditor::getBlockAtPosition(QPoint position) {
QTextBlock block = firstVisibleBlock();
while (true)
{
QRectF blockDim = blockBoundingGeometry(block).translated(contentOffset());
if (position.y() <= blockDim.bottom() && position.y() >= blockDim.top())
{
break;
}
else if (block.blockNumber() + 1 < blockCount())
block = block.next();
else break;
}
return new QTextBlock (block);
}
Well, this seems to work because when I add new lines (QTextBlocks) to the QPlainTextEdit by hitting Enter/Return the attributes of the object behind the pointer change as intended. Which means if u insert a line before the relevant block, the blockNumber increases and the geometry/position changes.
But if you delete the relevant line (backspace/del) the pointer still points to a QTextBlock in the QPlainTextEdit - I have no idea how or why. When I call the isValid() method on the pointer value it returns true.
So, why are the attributes changing tho it's a new QTextBlock object? Is there a way to get a direct pointer or a reference to a QTextBlock in the QPlainTextEdit?