How can I do this, when the user clicks the up or down arrow of the QSpinBox, the value will increase as the cursor moves, and the value will decrease when dragging down. I like this feature, very useful for users to be able to just click and drag the cursor than constantly click on errors. Here is the source code for a counter created in C # that works the way I would like in python. http://www.paulneale.com/tutorials/dotNet/numericUpDown/numericUpDown.htm
import sys
from PySide import QtGui, QtCore
class Wrap_Spinner( QtGui.QSpinBox ):
def __init__( self, minVal=0, maxVal=100, default=0):
super( Wrap_Spinner, self ).__init__()
self.drag_origin = None
self.setRange( minVal, maxVal )
self.setValue( default)
def get_is_dragging( self ):
return self.mouseGrabber( ) == self
def do_drag_start( self ):
self.drag_origin = QtGui.QCursor( ).pos( )
self.grabMouse( )
def do_drag_update( self ):
curPos = QtGui.QCursor( ).pos( )
offsetVal = self.drag_origin.y( ) - curPos.y( )
self.setValue( offsetVal )
print offsetVal
def do_drag_end( self ):
self.releaseMouse( )
self.drag_origin = None
def mousePressEvent( self, event ):
if QtCore.Qt.LeftButton:
print 'start drag'
self.do_drag_start( )
elif self.get_is_dragging( ) and QtCore.Qt.RightButton:
self.do_drag_end( )
else:
super( Wrap_Spinner, self ).mouseReleaseEvent( event )
def mouseMoveEvent( self, event ):
if self.get_is_dragging( ):
self.do_drag_update( )
else:
super( Wrap_Spinner, self ).mouseReleaseEvent( event )
def mouseReleaseEvent( self, event ):
if self.get_is_dragging( ) and QtCore.Qt.LeftButton:
print 'finish drag'
self.do_drag_end( )
else:
super( Wrap_Spinner, self ).mouseReleaseEvent( event )
class Example(QtGui.QWidget ):
def __init__( self):
super( Example, self ).__init__( )
self.initUI( )
def initUI( self ):
self.spinFrameCountA = Wrap_Spinner( 2, 50, 40)
self.spinB = Wrap_Spinner( 0, 100, 10)
self.positionLabel = QtGui.QLabel( 'POS:' )
grid = QtGui.QGridLayout( )
grid.setSpacing( 0 )
grid.addWidget( self.spinFrameCountA, 0, 0, 1, 1 )
grid.addWidget( self.spinB, 1, 0, 1, 1 )
grid.addWidget( self.positionLabel, 2, 0, 1, 1 )
self.setLayout( grid )
self.setGeometry( 800, 400, 200, 150 )
self.setWindowTitle( 'Max Style Spinner' )
self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
self.show( )
def main( ):
app = QtGui.QApplication( sys.argv )
ex = Example( )
sys.exit( app.exec_( ) )
if __name__ == '__main__':
main()
source
share