Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override tab width in qt?

Tags:

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.

like image 878
Barış Akkurt Avatar asked Oct 23 '12 09:10

Barış Akkurt


1 Answers

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(' '));
like image 75
Ferdinand Beyer Avatar answered Oct 16 '22 01:10

Ferdinand Beyer