Barış Akkurt Barış Akkurt - 1 year ago 179
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.

Answer Source

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(' '));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download