我找到了一个 C++ 示例,它将 QSpinBox 按钮方向修改为左右,并且可以正常工作。所以我用PyQt-4.7来模仿例子,但是按钮没有显示出来。下面是我的代码:
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QStyle, QCommonStyle
class ScientificDoubleSpinBox(QtGui.QDoubleSpinBox):
def __init__(self, *args, **kwargs):
super(ScientificDoubleSpinBox, self).__init__(*args, **kwargs)
self.setMinimum(0)
self.setMaximum(1000)
self.setDecimals(1000)
style = SpinBoxStyle()
#here
self.setStyle(style)
class SpinBoxStyle(QtGui.QCommonStyle):
def __init__(self):
super(SpinBoxStyle, self).__init__()
def drawComplexControl(self, control, option, painter, widget = None):
if(control == QtGui.QStyle.CC_SpinBox):
self.drawSpinBoxButton(QtGui.QStyle.SC_SpinBoxDown,option,painter);
self.drawSpinBoxButton(QtGui.QStyle.SC_SpinBoxUp,option,painter);
rect = self.subControlRect(QtGui.QStyle.CC_SpinBox,option,QtGui.QStyle.SC_SpinBoxEditField, widget).adjusted(-1,0,+1,0)
painter.setPen(QtGui.QPen(option.palette.mid(),1.0))
painter.drawLine(rect.topLeft(),rect.bottomLeft())
painter.drawLine(rect.topRight(),rect.bottomRight())
else:
return QCommonStyle().drawComplexControl(which,option,painter)
def drawSpinBoxButton(self, subControl, option, painter, widget = None):
buttonWidth = 16
arrow = QtGui.QStyle.PE_IndicatorSpinDown
buttonRect = option.rect
if((subControl==QtGui.QStyle.SC_SpinBoxUp)!=(option.direction==1)): #Qt.RightToLeft
arrow = QtGui.QStyle.PE_IndicatorSpinUp
buttonRect.translate(buttonRect.width()/2,0)
else:
buttonRect.translate(buttonRect.width()/2-buttonWidth,0)
buttonRect.setWidth((buttonRect.width()+1)*2);
#????
#buttonOpt = qstyleoption_cast(option)
buttonOpt = QtGui.QStyleOption(option)
painter.save()
painter.setClipRect(buttonRect, 2) #Qt.IntersectClip
#????
#if(not(option.activeSubControls & subControl)):
#buttonOpt.state &=not(QtGui.QStyle.State_MouseOver|QtGui.QStyle.State_On|QtGui.QStyle.State_Sunken)
arrowOpt= QtGui.QStyleOption(buttonOpt)
arrowOpt.rect = self.subControlRect(QtGui.QStyle.CC_SpinBox,option,subControl, widget);
if(arrowOpt.rect.isValid()):
self.drawPrimitive(arrow,arrowOpt,painter)
painter.restore()
def subControlRect(self, whichControl, option, whichSubControl, widget):
if(whichControl==QtGui.QStyle.CC_SpinBox):
frameWidth = self.pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth, option, widget)
buttonWidth = 16;
if whichSubControl == QtGui.QStyle.SC_SpinBoxFrame:
return option.rect
elif whichSubControl == QtGui.QStyle.SC_SpinBoxEditField:
return option.rect.adjusted(0, +frameWidth,-buttonWidth*4,-frameWidth)
elif whichControl == QtGui.QStyle.SC_SpinBoxDown:
return self.visualRect(option.direction, option.rect, QtCore.QRect(option.rect.right()-buttonWidth*2,
option.rect.y(), buttonWidth, option.rect.height()))
elif whichControl == QtGui.QStyle.SC_SpinBoxUp:
return self.visualRect(option.direction, option.rect, QtCore.QRect(option.rect.right()-buttonWidth,
option.rect.y(), buttonWidth, option.rect.height()))
else:
return QtCore.QRect()
else:
return QCommonStyle.subControlRect(whichControl,option,whichSubControl)