I want to load some part of the very big file into my QListWidget on Python PyQt. When user moves the scrollbar of the QListWidget and reaches the end of the scrollbar, the event is activating and the next part of the file is loading (appends) to the QListWidget. Is there some event for controlling the end position of the scrollbar?
1 Answer
There's no dedicated signal for "scrolled to end", but you can easily check that in the valueChanged signal:
def scrolled(scrollbar, value):
if value == scrollbar.maximum():
print 'reached max' # that will be the bottom/right end
if value == scrollbar.minimum():
print 'reached min' # top/left end
scrollBar = listview.verticalScrollBar()
scrollBar.valueChanged.connect(lambda value: scrolled(scrollBar, value))
EDIT:
Or, within a class:
class MyWidget(QWidget):
def __init__(self):
# here goes the rest of your initialization code
# like the construction of your listview
# connect the valueChanged signal:
self.listview.verticalScrollBar().valueChanged.connect(self.scrolled)
# your parameter "f"
self.f = 'somevalue' # whatever
def scrolled(self, value):
if value == self.listview.verticalScrollBar().maximum():
self.loadNextChunkOfData()
def loadNextChunkOfData(self):
# load the next piece of data and append to the listview
You should probably catch up on the docs for lambda's and the signal-slot framework in general.
5 Comments
Gooman
How I can use this lambda expression in order to give another 3 argument to the scrolled function?
sebastian
You could simply write the following:
lambda value: scrolled(scrollBar, someParameter, value). The lambda however was more for the purpose of this example. Ideally you'd put the above code into a class, where you can connect the valueChanged signal to a method of this class instead of using a "standalone" function.Gooman
It's a problem to write
lambda value: scrolled(scrollBar, someParameter, value) because I need to give self as a first parameter.Gooman
I trying to use it like this:
lambda value: self.scrolled(self, scrollBar, f, value). And I receiving an error: TypeError: scrolled() takes exactly 4 arguments (5 given).sebastian
If
scrolled is a method within your class the following lambda will work: lambda value: self.scrolled(scrollBar, f, value). Though if f and the scrollbar are accessible from self there's no need for that... see my edit.