This is purely hypothetical as I can't tell you how to solve your problem with any certainty, but I would have started by looking into connecting the QTextEdit::textChanged signal to a slot that would call QWidget::setContentsMargins with values obtained from your max width calculation.