如何调整 QSCrollBar 的大小以在运行时更改它的宽度?
How can I resize a QSCrollBar to change it's width in runtime?
我目前正在尝试在 Qt (python) 中重现 Window 10 开始菜单滚动条,但我不知道如何调整自定义 QScrollBar 的大小以在运行时更改它的宽度.
我尝试使用 QScrollBar.resize 方法(在 enterEvent 和 leaveEvent 中)调整它的大小,但它将小部件缩放到它的“绘图区域”之外。
例如,我的滚动条设置为 QScrollArea,当我尝试调整它的大小时,它不需要更多 space 和移动小部件,而不是它只是在它的右边缩放,我不能看到了。
我目前找到的唯一解决方案是使用 StyleSheet,但我无法将其设置为动画以实现我正在寻找的平滑调整大小。
有一些代码供您测试,看看有什么问题:
from PySide2 import QtWidgets, QtCore
from functools import partial
class MyTE(QtWidgets.QPlainTextEdit):
def __init__(self):
super(MyTE, self).__init__()
self.setVerticalScrollBar(MyScrollBar(self))
self.setPlainText('mmmmmmmmmmmmmmmmmmmmmmmmmmmmm'*50)
class MyScrollBar(QtWidgets.QScrollBar):
def __init__(self, parent=None):
super(MyScrollBar, self).__init__(parent=parent)
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
def enterEvent(self, event):
super(MyScrollBar, self).enterEvent(event)
self.resize(QtCore.QSize(4, self.height()))
def leaveEvent(self, event):
super(MyScrollBar, self).leaveEvent(event)
self.resize(QtCore.QSize(10, self.height()))
wid = MyTE()
wid.show()
为了使宽度变化更平滑,您可以使用 QVariantAnimation
:
from PySide2 import QtWidgets, QtCore
class MyTE(QtWidgets.QPlainTextEdit):
def __init__(self):
super(MyTE, self).__init__()
self.setVerticalScrollBar(MyScrollBar(self))
self.setPlainText("mmmmmmmmmmmmmmmmmmmmmmmmmmmmm" * 50)
class MyScrollBar(QtWidgets.QScrollBar):
def __init__(self, parent=None):
super(MyScrollBar, self).__init__(parent=parent)
self._animation = QtCore.QVariantAnimation(
startValue=10, endValue=25, duration=500
)
self._animation.valueChanged.connect(self.change_width)
def enterEvent(self, event):
super(MyScrollBar, self).enterEvent(event)
self._animation.setDirection(QtCore.QAbstractAnimation.Forward)
self._animation.start()
def leaveEvent(self, event):
super(MyScrollBar, self).leaveEvent(event)
self._animation.setDirection(QtCore.QAbstractAnimation.Backward)
self._animation.start()
def change_width(self, width):
self.setStyleSheet("""QScrollBar:vertical{ width: %dpx;}""" % (width,))
if __name__ == "__main__":
app = QtWidgets.QApplication()
wid = MyTE()
wid.show()
app.exec_()
.0我终于找到了不使用样式表的方法!
感谢 eyllanesc 的回答,它给了我一个新的工作基础,我终于明白了 Qt 如何处理调整大小(我猜),我在每次动画值变化时调用的方法中使用 setFixedSize
.
要工作,这需要有一个重写的 sizeHint
方法,该方法 return 动画的宽度值。
此外,这适用于 Autodesk Maya(不幸的是,eyllanesc 提供的解决方案由于未知原因在 Maya 中不起作用)。
有我的解决办法:
from PySide2 import QtWidgets, QtCore, QtGui
class MyTE(QtWidgets.QPlainTextEdit):
def __init__(self):
super(MyTE, self).__init__()
self.setVerticalScrollBar(MyScrollBar(self))
self.setPlainText("mmmmmmmmmmmmmmmmmmmmmmmmmmmmm" * 50)
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
def resizeEvent(self, event):
super(MyTE, self).resizeEvent(event)
class MyScrollBar(QtWidgets.QScrollBar):
def __init__(self, parent=None):
super(MyScrollBar, self).__init__(parent=parent)
self._animation = QtCore.QVariantAnimation(
startValue=10.0, endValue=25.0, duration=300
)
self._animation.valueChanged.connect(self.changeWidth)
self._width = 10
def enterEvent(self, event):
super(MyScrollBar, self).enterEvent(event)
self._animation.setDirection(QtCore.QAbstractAnimation.Forward)
self._animation.start()
def leaveEvent(self, event):
super(MyScrollBar, self).leaveEvent(event)
self._animation.setDirection(QtCore.QAbstractAnimation.Backward)
self._animation.start()
def sizeHint(self):
"""This must be overrided and return the width processed by the animation
.. note:: In my final version I've replaced self.height() with 1 because it does not change
anything but self.height() was making my widget grow a bit each time.
"""
return QtCore.QSize(self._width, self.height())
def changeWidth(self, width):
self._width = width # This will allow us to return this value as the sizeHint width
self.setFixedWidth(width) # This will ensure to scale the widget properly.
if __name__ == "__main__":
app = QtWidgets.QApplication()
wid = MyTE()
wid.show()
app.exec_()
注意:QVariantAnimation 的 startValue 和 endValue 必须是 float,如果它们是 int 类型动画将无法工作(适用于 Qt 5.6.1 (Maya 2018) 但不适用于 Qt 5.12.5 (Maya 2020)
PS: 如果有人对我的最终Widget(Window 10 开始菜单滚动条)感兴趣,请私下问我。
我目前正在尝试在 Qt (python) 中重现 Window 10 开始菜单滚动条,但我不知道如何调整自定义 QScrollBar 的大小以在运行时更改它的宽度.
我尝试使用 QScrollBar.resize 方法(在 enterEvent 和 leaveEvent 中)调整它的大小,但它将小部件缩放到它的“绘图区域”之外。 例如,我的滚动条设置为 QScrollArea,当我尝试调整它的大小时,它不需要更多 space 和移动小部件,而不是它只是在它的右边缩放,我不能看到了。
我目前找到的唯一解决方案是使用 StyleSheet,但我无法将其设置为动画以实现我正在寻找的平滑调整大小。
有一些代码供您测试,看看有什么问题:
from PySide2 import QtWidgets, QtCore
from functools import partial
class MyTE(QtWidgets.QPlainTextEdit):
def __init__(self):
super(MyTE, self).__init__()
self.setVerticalScrollBar(MyScrollBar(self))
self.setPlainText('mmmmmmmmmmmmmmmmmmmmmmmmmmmmm'*50)
class MyScrollBar(QtWidgets.QScrollBar):
def __init__(self, parent=None):
super(MyScrollBar, self).__init__(parent=parent)
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
def enterEvent(self, event):
super(MyScrollBar, self).enterEvent(event)
self.resize(QtCore.QSize(4, self.height()))
def leaveEvent(self, event):
super(MyScrollBar, self).leaveEvent(event)
self.resize(QtCore.QSize(10, self.height()))
wid = MyTE()
wid.show()
为了使宽度变化更平滑,您可以使用 QVariantAnimation
:
from PySide2 import QtWidgets, QtCore
class MyTE(QtWidgets.QPlainTextEdit):
def __init__(self):
super(MyTE, self).__init__()
self.setVerticalScrollBar(MyScrollBar(self))
self.setPlainText("mmmmmmmmmmmmmmmmmmmmmmmmmmmmm" * 50)
class MyScrollBar(QtWidgets.QScrollBar):
def __init__(self, parent=None):
super(MyScrollBar, self).__init__(parent=parent)
self._animation = QtCore.QVariantAnimation(
startValue=10, endValue=25, duration=500
)
self._animation.valueChanged.connect(self.change_width)
def enterEvent(self, event):
super(MyScrollBar, self).enterEvent(event)
self._animation.setDirection(QtCore.QAbstractAnimation.Forward)
self._animation.start()
def leaveEvent(self, event):
super(MyScrollBar, self).leaveEvent(event)
self._animation.setDirection(QtCore.QAbstractAnimation.Backward)
self._animation.start()
def change_width(self, width):
self.setStyleSheet("""QScrollBar:vertical{ width: %dpx;}""" % (width,))
if __name__ == "__main__":
app = QtWidgets.QApplication()
wid = MyTE()
wid.show()
app.exec_()
.0我终于找到了不使用样式表的方法!
感谢 eyllanesc 的回答,它给了我一个新的工作基础,我终于明白了 Qt 如何处理调整大小(我猜),我在每次动画值变化时调用的方法中使用 setFixedSize
.
要工作,这需要有一个重写的 sizeHint
方法,该方法 return 动画的宽度值。
此外,这适用于 Autodesk Maya(不幸的是,eyllanesc 提供的解决方案由于未知原因在 Maya 中不起作用)。
有我的解决办法:
from PySide2 import QtWidgets, QtCore, QtGui
class MyTE(QtWidgets.QPlainTextEdit):
def __init__(self):
super(MyTE, self).__init__()
self.setVerticalScrollBar(MyScrollBar(self))
self.setPlainText("mmmmmmmmmmmmmmmmmmmmmmmmmmmmm" * 50)
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
def resizeEvent(self, event):
super(MyTE, self).resizeEvent(event)
class MyScrollBar(QtWidgets.QScrollBar):
def __init__(self, parent=None):
super(MyScrollBar, self).__init__(parent=parent)
self._animation = QtCore.QVariantAnimation(
startValue=10.0, endValue=25.0, duration=300
)
self._animation.valueChanged.connect(self.changeWidth)
self._width = 10
def enterEvent(self, event):
super(MyScrollBar, self).enterEvent(event)
self._animation.setDirection(QtCore.QAbstractAnimation.Forward)
self._animation.start()
def leaveEvent(self, event):
super(MyScrollBar, self).leaveEvent(event)
self._animation.setDirection(QtCore.QAbstractAnimation.Backward)
self._animation.start()
def sizeHint(self):
"""This must be overrided and return the width processed by the animation
.. note:: In my final version I've replaced self.height() with 1 because it does not change
anything but self.height() was making my widget grow a bit each time.
"""
return QtCore.QSize(self._width, self.height())
def changeWidth(self, width):
self._width = width # This will allow us to return this value as the sizeHint width
self.setFixedWidth(width) # This will ensure to scale the widget properly.
if __name__ == "__main__":
app = QtWidgets.QApplication()
wid = MyTE()
wid.show()
app.exec_()
注意:QVariantAnimation 的 startValue 和 endValue 必须是 float,如果它们是 int 类型动画将无法工作(适用于 Qt 5.6.1 (Maya 2018) 但不适用于 Qt 5.12.5 (Maya 2020)
PS: 如果有人对我的最终Widget(Window 10 开始菜单滚动条)感兴趣,请私下问我。