Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Custom scroll bar for QTextEdit
zerg2011 last edited by
I have a text file of a huge size , and I need to show it in QTextEdit.
But simple loading the whole text into the QTextEdit widget , consume huge amount of time , freeze event loop and so on.
So , instead of straightforward loading text into QTextEdit , I load text by a small chunks .
But the default QTextEdit scroll bar view is dependent from the text , the size and position of scroll bar slider shows the real size of
Thus , I want to set into QTextEdit other scroll bar to achieve scroll bar behaviour , as if I load a whole file.
Firstly I tried to use
@ QAbstractScrollArea::setVerticalScrollBar ( QScrollBar * scrollBar ) @
But result was exactly the same as with a default scroll bar - scroll bar is still dependent from the text.
In the end I added the QScrollBar as a child to QTextEdit like this :
QTextEdit * textEdit ;
QScrollBar *sb = new QScrollBar( textEdit ) ;
sb -> show() ;
Now the scroll bar slider is independent from the text , and it's good
, but I have to synchronize scroll bar size and position with parent widget manually, and it's bad.
My question - what is correct way to set into QTextEdit custom scroll bar
, is such , that from one hand does not depend from the QTextEdit content , and from the other hand depend from its size.
( I've tried to put my scroll bar into internal QTextEdit layout , but I can't get access to it. )
rschaub last edited by
I think as soon as a scrollbar is added to a textedit, the edit takes control over it and sets the scrollbar min and max values.
If you want to achieve incremental loading, I'd place a separate scrollbar widget next to the text edit, manually set its min and max values and call a custom slot when moving the scrollbar.
In that slot, you can incrementally load the text information based on your scrollbar position and display the text in the textedit.
You'd just have to make sure the text you're displaying doesn't cause the textedit to show additional scrollbars, else it would look silly ;)
andre last edited by
I think you have two ways of going:
Subclass QScrollBar, and basically give it a second set of min and max properties. Use those properties instead of the default for interaction and rendering (at the very least you'll have to re-implement the drawing and mouse handling stuff for that). Then, set this scroll bar as the new scroll bar to use. Not easy.
Alternatively, you could hide the original scroll bar of the text edit, and create a new one that you don't set on it. Instead, you use a of QSS (via the setStyleSheet method) to create a bit of padding inside the text edit. In that area you manually set your own QScrollBar. You will need to handle it's geometry manually, by using the resizeEvent of the text edit. You can then use the same methods as rschaub describes to manipulate the real scroll position.
I think option 2 is much simpler to pull off.