Issue
I've got a QPlainTextEdit object which I'm using as a mini-python editor in one of my tools. The problem is that when you insert a tab in QPlainTextEdit, it inserts a great big tab rather than four spaces like I'd prefer.
Is there a way to override this functionality, like you can in programs like Notepad++?
Solution
Use the setTabStopWidth(width)
method to change how much space one tab takes. It takes width in pixels. The default is 80.
If you need to replace all the tabs with four spaces, you can get the string from qtextedit and then replace them
s= text_edit.toPlainText()
s= s.replace('\t', ' ')
Changing tab width in QTextEdit
:
from PyQt5.QtWidgets import *
import sys
class Wind(QWidget):
def __init__(self):
super().__init__()
self.setupUI()
def setupUI(self):
self.setGeometry(300,300, 300,500)
self.show()
vb_layout= QVBoxLayout(self)
text_edit= QTextEdit(self)
# This method sets tab width in pixels
text_edit.setTabStopWidth(8)
vb_layout.addWidget(text_edit)
self.setLayout(vb_layout)
def main():
app= QApplication(sys.argv)
w = Wind()
exit(app.exec_())
if __name__ == '__main__':
main()
Answered By - Anonta
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.