[Gambas-user] Tab size in a textarea

T Lee Davidson t.lee.davidson at gmail.com
Sat Nov 5 18:05:33 CET 2022

On 11/5/22 02:43, BB wrote:
> Is there any way to set the tab size in a TextArea control?
> I mean the width on the left when I hit the Tab key when editing the TextArea text.
> Mainly interested in QT5 but regardless of the gui it is "Huge", about 10 characters.
> tia
> bruce

It appears that having the ability to set the tab size would require modification of the TextArea code[1].

Since tabStopDistance is specified in pixels, the font metrics would need to be taken into account. An example from 
StackExchange[2] for Python:
textedit = QtWidgets.QPlainTextEdit()
textedit.setTabStopDistance( QtGui.QFontMetricsF(textedit.font() ).horizontalAdvance(' ') * 4)

I don't know how that would translate to a Gambas TextArea.

In the meantime, you could manipulate TextArea.Text yourself:
' Gambas class file

Private iTabSize As Integer = 4
Private bTabKey As Boolean

Public Sub TextArea1_Change()

   If bTabKey Then
     bTabKey = False
     ' TextArea1.Undo ' Does not work; Undo is greedy taking more than just the last keystroke.
     ' TextArea1.Insert("\b") ' Neither the backspace control character nor Chr(8) behave as expected.
     TextArea1.Text = String.Left(TextArea1.Text, -1)


Public Sub TextArea1_KeyPress()

   If Key.Code = Key.Tab Then ' Do not edit TextArea.Text in this event.
     bTabKey = True ' Use a semaphore because Key information is not available in the Change event.



[1] https://doc.qt.io/qt-5/qplaintextedit.html#tabStopDistance-prop
[2] https://stackoverflow.com/questions/50190860/how-to-change-the-width-of-tabs-in-a-qplaintextedit

More information about the User mailing list