QTabWidget中QListWidget上的QScroller不起作用

时间:2019-07-16 13:15:09

标签: python pyqt pyqt5

简而言之,我有QTabWidget包含tab1和tab2。 tab1和tab2均包含一个QListWidget。我想在每个QListWidget上使用QScroller。

这是代码(最简单的版本)。

import sys

from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QAbstractItemView, QScrollerProperties, QScroller, QVBoxLayout, QListWidget,
                             QTabWidget, QApplication, QLabel, QListWidgetItem)
from PyQt5.QtCore import Qt


class MainWindow(QWidget):

    def __init__(self):
        super().__init__()

        self.resize(700, 300)

        mainLayout = QVBoxLayout()
        self.tabWidget = QTabWidget()
        self.tabWidget.setStyleSheet("QTabBar::tab { height: 50px; width: 250px; }")

        mainLayout.addWidget(self.tabWidget)

        myBoxLayout = QHBoxLayout()
        self.tabWidget.setLayout(myBoxLayout)

        self.tab1 = WidgetTab1()
        self.tab2 = WidgetTab2()

        self.tabWidget.addTab(self.tab1, 'Tab1')
        self.tabWidget.addTab(self.tab2, 'Tab2')
        self.setLayout(mainLayout)

class WidgetTab1(QWidget):

    def __init__(self):
        super().__init__()
        self.hbox = QHBoxLayout()

        # Create the list
        self.mylist = QListWidget()
        self.mylist.setStyleSheet("QListWidget::item { border-bottom: 1px solid gray; }")
        self.mylist.setFocusPolicy(Qt.NoFocus)
        self.mylist.setSelectionMode(QAbstractItemView.NoSelection)
        self.mylist.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.mylist.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel)

        for i in range(20):
            item = QListWidgetItem(self.mylist)
            self.mylist.addItem(item)
            self.mylist.setItemWidget(item, QLabel(str(i)))

        self.sp = QScrollerProperties()
        self.sp.setScrollMetric(QScrollerProperties.DragVelocitySmoothingFactor, 0.6)
        self.sp.setScrollMetric(QScrollerProperties.MinimumVelocity, 0.0)
        self.sp.setScrollMetric(QScrollerProperties.MaximumVelocity, 0.2)
        self.sp.setScrollMetric(QScrollerProperties.AcceleratingFlickMaximumTime, 0.1)
        self.sp.setScrollMetric(QScrollerProperties.AcceleratingFlickSpeedupFactor, 1.2)
        self.sp.setScrollMetric(QScrollerProperties.SnapPositionRatio, 0.2)
        self.sp.setScrollMetric(QScrollerProperties.MaximumClickThroughVelocity, 1)
        self.sp.setScrollMetric(QScrollerProperties.DragStartDistance, 0.001)
        self.sp.setScrollMetric(QScrollerProperties.MousePressEventDelay, 0.5)

        self.scroller = QScroller.scroller(self.mylist.viewport())
        self.scroller.setScrollerProperties(self.sp)
        self.scroller.grabGesture(self.mylist.viewport(), QScroller.LeftMouseButtonGesture)

        self.mylist.show()
        self.hbox.addWidget(self.mylist)
        self.setLayout(self.hbox)

class WidgetTab2(QWidget):

    def __init__(self):
        super().__init__()
        self.hbox = QHBoxLayout()

        # Create the list
        self.mylist = QListWidget()
        self.mylist.setStyleSheet("QListWidget::item { border-bottom: 1px solid gray; }")
        self.mylist.setFocusPolicy(Qt.NoFocus)
        self.mylist.setSelectionMode(QAbstractItemView.NoSelection)
        self.mylist.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.mylist.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel)

        for i in range(19, 0, -1):
            item = QListWidgetItem(self.mylist)
            self.mylist.addItem(item)
            self.mylist.setItemWidget(item, QLabel(str(i)))

        self.sp = QScrollerProperties()
        self.sp.setScrollMetric(QScrollerProperties.DragVelocitySmoothingFactor, 0.6)
        self.sp.setScrollMetric(QScrollerProperties.MinimumVelocity, 0.0)
        self.sp.setScrollMetric(QScrollerProperties.MaximumVelocity, 0.2)
        self.sp.setScrollMetric(QScrollerProperties.AcceleratingFlickMaximumTime, 0.1)
        self.sp.setScrollMetric(QScrollerProperties.AcceleratingFlickSpeedupFactor, 1.2)
        self.sp.setScrollMetric(QScrollerProperties.SnapPositionRatio, 0.2)
        self.sp.setScrollMetric(QScrollerProperties.MaximumClickThroughVelocity, 1)
        self.sp.setScrollMetric(QScrollerProperties.DragStartDistance, 0.001)
        self.sp.setScrollMetric(QScrollerProperties.MousePressEventDelay, 0.5)

        self.scroller = QScroller.scroller(self.mylist.viewport())
        self.scroller.setScrollerProperties(self.sp)
        self.scroller.grabGesture(self.mylist.viewport(), QScroller.LeftMouseButtonGesture)

        self.mylist.show()
        self.hbox.addWidget(self.mylist)
        self.setLayout(self.hbox)

if __name__ == '__main__':
    qApplication = QApplication(sys.argv)

    window = MainWindow()
    window.show()
    qApplication.exec_()

问题是,QScroller仅在第一个选项卡中起作用。例如,如果我首先选择tab1并在其上滚动。然后我切换到tab2,在tab2上的滚动将不起作用。如果我先选择tab2并在其上滚动。然后我切换到tab1,在tab1上的滚动将不起作用。

每当我切换标签时,我也尝试过ungrabGesture,但是它不起作用。

我是否以错误的方式实现了QScroller?

Behaviour Description

1 个答案:

答案 0 :(得分:0)

感谢@ G.M。供您评论。

我通过将PyQt版本从5.11.3升级到5.13.0解决了问题