How to override tab width in qt?

While @Ferdinand Beyer's solution will work on some systems, generally fonts are not guaranteed to have integer metrics. e.g 12pt DejaVu Sans Mono on my Linux setup has character width of 9.625. The best solution I've found is add some letter spacing to get pixel-perfect alignment.

int tabstop = 4;

QFontMetricsF fm (ui->textEdit->font());
auto stopWidth = tabstop * fm.width(' ');
auto letterSpacing = (ceil(stopWidth) - stopWidth) / tabstop;

auto font = ui->textEdit->font();
font.setLetterSpacing(QFont::AbsoluteSpacing, letterSpacing);
ui->textEdit->setFont(font);

ui->textEdit->setTabStopWidth(ceil(stopWidth));

While the question about how to set tab stop width has been answered already; computing the correct tab width in pixels is still (or again?) an open question.

Since Qt 5.10, QTextEdit::tabStopWidth is marked as obsolete and QTextEdit::tabStopDistance was introduced. tabStopWidth was integer, tabStopDistance is double.

Why so complicated?

Setting n * QFontMetrics::width(' ') as tab stop width makes troubles because font_metrics.width returns an integer. Even if you have a monospace standard font, the width of a single character is actually not an integer, so QFontMetrics::width returns an inaccurate measure. If you compare the appearance of the strings ........| and \t\t\t\t| (\t = tab, n=2), you will see that the pipes are not aligned properly. It will become worse the more tabs you insert.

Solution

You could do what @Ferdinand Beyer proposed, but it will slightly change the typeface. I also had to adapt his method to make it work. However, there's a much simpler approach which exploits that you can set tabStopDistance now with double precision:

static constexpr int tab_width_char = 2;
m_text_edit->setFont(QFont("Courier", 12));
const auto font_metrics = m_text_edit->fontMetrics();

static constexpr int big_number = 1000; // arbitrary big number.
const QString test_string(" ");

// compute the size of a char in double-precision
const int single_char_width = font_metrics.width(test_string);
const int many_char_width = font_metrics.width(test_string.repeated(big_number));
const double single_char_width_double = many_char_width / double(big_number);

// set the tab stop with double precision
m_text_edit->setTabStopDistance(tab_width_char * single_char_width_double);

This would be so much simpler if Qt offered a way to get the width of a single character as double.


The QTextEdit::tabStopWidth property might solve your problem (see here for Documentation...)


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;
font.setFamily("Courier");
font.setStyleHint(QFont::Monospace);
font.setFixedPitch(true);
font.setPointSize(10);

QTextEdit* editor = new QTextEdit();
editor->setFont(font);

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(' '));