[Python] Poor responsiveness of a QScrollBar handle [duplicate]

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    Not sure what is the issue: I am using list view and I struggle with the scrollbar. Somehow, the handle part is not always responsive. Sometimes I can click the handle and scroll fast to the place which I need, sometimes I cannot. I made a MRE from it and I could not identify if there are some specific conditions when it does or does not happen. I can always scroll with the mouse wheel, so it does not seem to be an app freeze. But I cannot always grab the handle and drag it. MRE is below. I am on Windows 11, using PySide2. Not sure if the problem is on my machine (though what can be wrong for a scrollbar?) or in some way the scrollbar works. It is as if it's handle is not always active.

    import sys
    import os
    from PySide2.QtGui import QPixmap, QIcon, QStandardItemModel, QStandardItem
    from PySide2.QtWidgets import (
    QApplication,
    QWidget,
    QVBoxLayout,
    QListView,
    )
    from PySide2.QtCore import Qt, QDir, QSize


    class Example(QWidget):
    def __init__(self):
    super().__init__()
    self.layout = QVBoxLayout(self)

    # File list view setup
    self.file_list_view = QListView(self)
    self.file_list_view.setFixedHeight(200)
    self.layout.addWidget(self.file_list_view)

    # File model
    self.file_model = QStandardItemModel(self)
    self.file_list_view.setModel(self.file_model)

    # Custom scrollbar style
    self.file_list_view.verticalScrollBar().setStyleSheet(
    """
    QScrollBar:vertical {
    width: 30px;
    background-color: transparent;
    border: none;
    }
    QScrollBar::handle:vertical {
    background-color: rgb(100,100,100);
    border-radius: 5px;
    min-height: 50px;
    }
    QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {
    border: none;
    background: transparent;
    }
    QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical {
    background: transparent;
    }
    """
    )

    def populate_directory_view(self, dir_path):
    self.file_model.clear()
    self.dir_path = str(dir_path)

    # Use os.listdir to get the file names (instead of QDir)
    files = os.listdir(self.dir_path)
    for file in files:
    file_item = QStandardItem(file)
    file_item.setTextAlignment(Qt.AlignCenter) # Center the text
    file_item.setEditable(False)
    self.file_model.appendRow(file_item)

    # Set the view mode and remove icons
    self.file_list_view.setViewMode(QListView.ListMode)
    self.file_list_view.setIconSize(QSize(0, 0))

    def reset(self):
    self.file_model.clear()
    self.dir_path = ""


    if __name__ == "__main__":
    app = QApplication(sys.argv)
    viewer = Example()
    viewer.show()

    # Test directory path (use any valid directory on your machine)
    test_directory = ... # Replace with your own test directory path
    viewer.populate_directory_view(test_directory)

    sys.exit(app.exec_())


    Would be nice if a completely unrelated question would not be marked as duplicate of mine. I want to customize the handle.

    Continue reading...

Compartilhe esta Página