Barış Akkurt Barış Akkurt - 8 months ago 79
C++ Question

How to override tab width in qt?

I just need to know how to change the tab size in Qt in a QTextEdit. My Google and stackoverflow search returned me null. Thanks in advance.


If you want to create a source code editor using QTextEdit, you should first assign a fixed-width (monospace) font. This ensures that all characters have the same width:

QFont font;

QTextEdit* editor = new QTextEdit();

If you want to set a tab width to certain amount of spaces, as it is typically done in text editors, use QFontMetrics to compute the size of one space in pixels:

const int tabStop = 4;  // 4 characters

QFontMetrics metrics(font);
editor->setTabStopWidth(tabStop * metrics.width(' '));