Umutins62
Umutins62

Reputation: 23

automatic scroll bar down in QLisWidget

In the application I prepared with Python PyQT5, when I add an item to the QLisWidget, I want the verticalscroll bar to automatically scroll down.

Upvotes: 1

Views: 574

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

You have to use scrollToItem() method to make the scrollbar move:

import sys

from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QListWidget, QListWidgetItem


def main():
    app = QApplication(sys.argv)

    w = QListWidget()
    w.resize(320, 240)
    w.show()

    def on_timeout():
        item = QListWidgetItem(f"item-{w.count()}")
        w.addItem(item)
        w.scrollToItem(item)

    timer = QTimer(interval=1000, timeout=on_timeout)
    timer.start()

    sys.exit(app.exec_())


main()

Upvotes: 2

Related Questions