在不改变应用风格的情况下改变 QSpinBox 箭头大小
Change QSpinBox Arrow Size Without Changing App Style
我正在尝试找到一种方法来更改小部件的属性,例如组件的大小或各种边框和颜色,而不更改小部件的默认样式(在本例中为融合)。仍在尝试了解 PyQT 中的样式表,有人可以解释如何在不改变样式的情况下实现更大的箭头按钮吗?
提前致谢。
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("fusion")
#Trying to change the arrow size here, while maintaining their "fusion" style
app.setStyleSheet("QSpinBox::up-button { width: 32px; }"
"QSpinBox::down-button { width: 32px; }")
windowExample = basicWindow()
windowExample.show()
sys.exit(app.exec_())
我也在为我的滑块使用代理样式以改变滑块大小,希望旋转框有类似的东西:
class SliderProxyStyle(QProxyStyle):
def pixelMetric(self, metric, option, widget):
if metric == QStyle.PM_SliderThickness:
return 20
elif metric == QStyle.PM_SliderLength:
return 40
return super().pixelMetric(metric, option, widget)
你不能,不能使用简单的样式表,正如 sub-controls documentation:
底部所解释的那样
With complex widgets such as QComboBox and QScrollBar, if one property or sub-control is customized, all the other properties or sub-controls must be customized as well."
复杂的widgets还包括QAbstractSpinBox的所有子类,QSpinBox继承自该子类,所以如果你改变任何属性(背景和前景色除外),你必须提供其他一切,作为样式的基本实现将使用基本 QCommonStyle 作为后备来忽略。
唯一可行的解决方案是使用 QProxyStyle 并为 SC_SpinBoxUp、SC_SpinBoxDown 和 SC_SpinBoxEditField
:
实现 subControlRect
class Proxy(QtWidgets.QProxyStyle):
def subControlRect(self, control, opt, subControl, widget=None):
rect = super().subControlRect(control, opt, subControl, widget)
if control == self.CC_SpinBox:
if subControl in (self.SC_SpinBoxUp, self.SC_SpinBoxDown):
rect.setLeft(opt.rect.width() - 32)
elif subControl == self.SC_SpinBoxEditField:
rect.setRight(opt.rect.width() - 32)
return rect
# ...
app = QtWidgets.QApplication(sys.argv)
app.setStyle(Proxy())
# ...
我正在尝试找到一种方法来更改小部件的属性,例如组件的大小或各种边框和颜色,而不更改小部件的默认样式(在本例中为融合)。仍在尝试了解 PyQT 中的样式表,有人可以解释如何在不改变样式的情况下实现更大的箭头按钮吗?
提前致谢。
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("fusion")
#Trying to change the arrow size here, while maintaining their "fusion" style
app.setStyleSheet("QSpinBox::up-button { width: 32px; }"
"QSpinBox::down-button { width: 32px; }")
windowExample = basicWindow()
windowExample.show()
sys.exit(app.exec_())
我也在为我的滑块使用代理样式以改变滑块大小,希望旋转框有类似的东西:
class SliderProxyStyle(QProxyStyle):
def pixelMetric(self, metric, option, widget):
if metric == QStyle.PM_SliderThickness:
return 20
elif metric == QStyle.PM_SliderLength:
return 40
return super().pixelMetric(metric, option, widget)
你不能,不能使用简单的样式表,正如 sub-controls documentation:
底部所解释的那样With complex widgets such as QComboBox and QScrollBar, if one property or sub-control is customized, all the other properties or sub-controls must be customized as well."
复杂的widgets还包括QAbstractSpinBox的所有子类,QSpinBox继承自该子类,所以如果你改变任何属性(背景和前景色除外),你必须提供其他一切,作为样式的基本实现将使用基本 QCommonStyle 作为后备来忽略。
唯一可行的解决方案是使用 QProxyStyle 并为 SC_SpinBoxUp、SC_SpinBoxDown 和 SC_SpinBoxEditField
:
class Proxy(QtWidgets.QProxyStyle):
def subControlRect(self, control, opt, subControl, widget=None):
rect = super().subControlRect(control, opt, subControl, widget)
if control == self.CC_SpinBox:
if subControl in (self.SC_SpinBoxUp, self.SC_SpinBoxDown):
rect.setLeft(opt.rect.width() - 32)
elif subControl == self.SC_SpinBoxEditField:
rect.setRight(opt.rect.width() - 32)
return rect
# ...
app = QtWidgets.QApplication(sys.argv)
app.setStyle(Proxy())
# ...