22

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.

pasbi
  • 2,037
  • 1
  • 20
  • 32
Barış Akkurt
  • 2,255
  • 3
  • 22
  • 37
  • 1
    How about `tabStopWidth` property? http://doc.qt.digia.com/qt/qtextedit.html#tabStopWidth-prop – fasked Oct 23 '12 at 09:17
  • I rejected [the suggested edit](https://stackoverflow.com/review/suggested-edits/22161002) by mistake, I think it's valid. – Al.G. Feb 09 '19 at 18:43

5 Answers5

41

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(' '));
Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
8

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

MartinStettner
  • 28,719
  • 15
  • 79
  • 106
  • 1
    thanks for your reply, to try it I set TabStopWidth to 40, so this changed my tab size. But this corresponds to 11 space characters and 5 a characters. Why all the charachters have different sizes? I want my editor to behave like source code editors. What i am missing? – Barış Akkurt Oct 24 '12 at 12:36
  • Use a monospace font like Courier New and all your characters will have the same width. – JonathanK Jan 16 '15 at 13:18
6

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.

pasbi
  • 2,037
  • 1
  • 20
  • 32
  • 1
    In at least Qt 5.15.2, I had to change font_metrics.width to font_metrics.horizontalAdvance, other than that this solution is the only one that worked for me! – foddex Oct 18 '21 at 12:45
5

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));
Ivan Baldin
  • 3,391
  • 3
  • 22
  • 14
4

Computing a product of a size of one space and num spaces is not always precise (tested under macOS, Monaco font), presumably due to some gaps in between the characters in the real string. A better solution would be to measure the length of the string containing tabStop spaces:

const int tabStop = 4;  // 4 characters

QString spaces;
for (int i = 0; i < tabStop; ++i) {
    spaces += " ";
}

QFontMetrics metrics(font);
editor->setTabStopWidth(metrics.width(spaces));