Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QScrollArea makes QTextEdit readonly

Tags:

qt

pyqt

qtextedit

I'm trying to add a container for showing the line numbers of a QTextEdit. From what I have seen so far I need to add a QAbstractScrollArea at the first step.

The problem is that when I add the QAbstractScrollArea, the QTextEdit render read-only. Almost read-only, I can drag&drop text but I don't have any cursor to input text.

Any ideeas? Thanks!

mainWindow = QMainWindow()
textEdit = QTextEdit(mainWindow)
textDocument = QTextDocument(textEdit)
...
# adding some text do textEdit
...

scrollArea = QAbstractScrollArea()
scrollArea.setViewport(textEdit)
scrollArea.setViewportMargins(20, 0, 0, 0)
mainWindow.setCentralWidget(scrollArea)
mainWindow.show()
like image 725
Daddy Pumpkin Avatar asked Mar 28 '26 14:03

Daddy Pumpkin


1 Answers

I'm not sure you should call setViewport at all.

QTextEdit already inherits from QAbstractScrollArea, so all you have to to is reserve the margin on its left, and either paint the line number or place a static widget that will display the line numbers in that margin.

There is already an example in the documentation (in C++) that does just that: Qt Code Editor example.

like image 50
alexisdm Avatar answered Apr 01 '26 06:04

alexisdm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!