void QTextDocumentPrivate::setBlockFormat(const QTextBlock &from, const QTextBlock &to, const QTextBlockFormat &newFormat, FormatChangeMode mode) { beginEditBlock(); Q_ASSERT(mode != SetFormatAndPreserveObjectIndices); // only implemented for setCharFormat Q_ASSERT(newFormat.isValid()); int newFormatIdx = -1; if (mode == SetFormat) newFormatIdx = formats.indexForFormat(newFormat); QTextBlockGroup *group = qobject_cast<QTextBlockGroup *>(objectForFormat(newFormat)); QTextBlock it = from; QTextBlock end = to; if (end.isValid()) end = end.next(); for (; it != end; it = it.next()) { int oldFormat = block(it)->format; QTextBlockFormat format = formats.blockFormat(oldFormat); QTextBlockGroup *oldGroup = qobject_cast<QTextBlockGroup *>(objectForFormat(format)); if (mode == MergeFormat) { format.merge(newFormat); newFormatIdx = formats.indexForFormat(format); group = qobject_cast<QTextBlockGroup *>(objectForFormat(format)); } block(it)->format = newFormatIdx; block(it)->invalidate(); QTextUndoCommand c = { QTextUndoCommand::BlockFormatChanged, true, QTextUndoCommand::MoveCursor, oldFormat, 0, it.position(), { 1 }, 0 }; appendUndoItem(c); if (group != oldGroup) { if (oldGroup) oldGroup->blockRemoved(it); if (group) group->blockInserted(it); } else if (group) { group->blockFormatChanged(it); } } documentChange(from.position(), to.position() + to.length() - from.position()); endEditBlock(); }