I have recently been working on a GUI that will be run on a touch screen in PySide2, and I wanted to implement scrolling by flicking on the touch screen. From what I could tell, the best way to do this would be to use a QScroller. I tried to implement it with this, but I did not like how the scroller would allow horizontal scrolling even though I had disabled the horizontal scroll bar. I saw online that it should be possible to disable this by modifying the scrollerProperties. However, whenever I try to get or set the scroller properties, the entire program immediately crashes. I have the following example, which reproduces my issue:
from PySide2.QtCore import QSize, QPoint, Qt
from PySide2.QtWidgets import QMainWindow, QWidget, QApplication, QLabel, QScrollArea, QScroller
class ScrollerCrash(QMainWindow):
def __init__(self):
QMainWindow.__init__(self, None)
self.setFixedSize(500, 500)
self._window = QWidget()
self.setCentralWidget(self._window)
self.labels = []
self.panel = QWidget(self)
self.panel.resize(QSize(500, 10000))
for i in range(0, 100):
label = QLabel(self.panel)
label.setText(f'Label {i}')
label.resize(QSize(500, 100))
label.move(QPoint(0, i * 100))
label.show()
self.labels.append(label)
self.scroll_area = QScrollArea(self)
self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll_area.horizontalScrollBar().setEnabled(False)
self.scroll_area.resize(QSize(500, 500))
self.scroll_area.show()
self.scroll_area.setWidget(self.panel)
self.scroller = QScroller(self.scroll_area)
self.scroller.grabGesture(self.scroll_area, QScroller.TouchGesture)
self.scroller.grabGesture(self.scroll_area, QScroller.LeftMouseButtonGesture)
print('before getting properties')
self.props = self.scroller.scrollerProperties() # Causes a seg fault
print('after getting properties')
if __name__ == '__main__':
application = QApplication()
test = ScrollerCrash()
test.show()
application.exec_()
My PySide2 version is 5.15.10.
I can't figure out any way to work around this. Is there something I am missing, or is there a better way to go about what I am doing? Thanks!
Thanks to musicamante, it was as simple as using QScroller.scroller() instead of directly creating a QScroller. I somehow missed that when I was going over the documentation.