I am making a pyqt widget to show bigger log files (1GB+). I will load them on demand. I have a Qtextbrowser in which I will read first N lines and after scrolling then i will load further. I don't want to use internal scrollbars of Qtetxbrowse as I have a separate lineNumber widget as well. hence I want to implement a custom vertical and hoxizontal scrollBar which will work as internal scrllbars. I have done vertical scrollBar implemeentation. I am facing follwoing issue while implementing Horzontal scrollBar
- Clicking on side of scrollbar doesn't scroll one character, instead it take 4-5 click to pass one character.
- Scrolling is too much slow with it
- Size(length) of horizontal scroll is not same as internal Horizontal scrollbar
class MyQTextBrowser(QTextBrowser):
resized = pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
def setVerticalScrollBar(self,obj):
self.VerticalScrollBar = obj
def resizeEvent(self,event):
super().resizeEvent(event)
self.resized.emit()
def wheelEvent(self, event):
self.VerticalScrollBar.wheelEvent(event)
super().wheelEvent(event)
def keyPressEvent(self,event):
self.VerticalScrollBar.keyPressEvent(event)
super().keyPressEvent(event)
class LazyQTextBrowser(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.__initUi()
def __initUi(self):
self.textBrowser = MyQTextBrowser(self)
self.textBrowser.setReadOnly(True)
self.textBrowser.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.vertical_scrollbar = QScrollBar(self)
self.horizontal_scrollbar = QScrollBar(Qt.Horizontal,self)
self.textBrowser.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.textBrowser.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.textBrowser.setVerticalScrollBar(self.vertical_scrollbar)
self.textBrowser.setVerticalScrollBar2(self.horizontal_scrollbar)
main_layout = QVBoxLayout()
text_edit_layout = QHBoxLayout()
text_edit_layout.addWidget(self.textBrowser)
text_edit_layout.addWidget(self.vertical_scrollbar)
main_layout.addLayout(text_edit_layout)
main_layout.addWidget(self.horizontal_scrollbar)
self.setLayout(main_layout)
self.textBrowser.resized.connect(self.textBrowserResized)
self.vertical_scrollbar.valueChanged.connect(self.textBrowser.verticalScrollBar().setValue)
self.horizontal_scrollbar.valueChanged.connect(lambda x : self.refreshData())
self.horizontal_scrollbar.setVisible(False)
def refreshData(self):
self.textBrowser.horizontalScrollBar().setValue(self.horizontal_scrollbar.value())
internal_slider_length = self.textBrowser.horizontalScrollBar().maximum()
if internal_slider_length > 0:
self.horizontal_scrollbar.setVisible(True)
self.horizontal_scrollbar.setRange(0, internal_slider_length)
# Slider height
internal_slider_height = self.textBrowser.horizontalScrollBar().sizeHint().height()
self.horizontal_scrollbar.setStyleSheet("QScrollBar:horizontal { height: " + str(internal_slider_height + 2) + "px; }")
# Slider length
self.horizontal_scrollbar.setPageStep(internal_slider_length)
else:
self.horizontal_scrollbar.setVisible(False)
