PDA

View Full Version : QSpinbox Button Access



mac_loo
22nd February 2011, 15:54
Hi,

I know this has been discussed several times here but I could not find any hint how to solve it actually. I would like to catch the mousepress event of a spin box in order to be able to reconfigure the box settings before the value actually changes. Background: my spinbox can support different ranges with different step sizes. So if one presses e.g. the up button I have to check before the actual value change whether a new range is to be entered or not (a new range would mean a new step size). For this reason I need to know whether up or down was pressed. Looking into the Qt code I can see that this decision is handled via internal hover states.

Is there any way to either get access to this hover state or to find out about the currently pressed button ?

Any hint appreciated.

cheers

m.

high_flyer
22nd February 2011, 16:37
I would like to catch the mousepress event of a spin box in order to be able to reconfigure the box settings before the value actually changes.
You can either subclass, or use an event filter.

mac_loo
22nd February 2011, 17:25
Well, that doesn't solve my problem. Of course I can subclass and catch the event but I don't know which button was pressed. I don't see a way to find out whether up or down button was pressed!

m.

high_flyer
22nd February 2011, 19:42
http://doc.trolltech.com/4.7/qspinbox.html#subclassing-qspinbox

mac_loo
22nd February 2011, 20:17
:-) I guess there's still some misunderstanding ... I need access to the up/down buttons of the spinbox. I have subclassed QSpinbox already but I have no way to identify whether up or down was pressed when in mousepressevent !

high_flyer
22nd February 2011, 21:31
you can tell by the value.
If the incoming value is larger than it was the up arrow, otherwise the down.

mac_loo
22nd February 2011, 23:03
Actually I can't as at the time I enter mousepressevent the current value has not changed yet. Step up or down is triggered in exactely this function.
I'm looking for a way to identify the pressed button ...

Added after 28 minutes:

Ok, finally found out how it work's !! (similar to QSpinBox but quite tricky to find out)

Subclass QSpinBox and overwrite QSpinBox::mousePressEvent.
Get the SubClass rectangle of the corresponding SpinBox arrow of the complex control first, then check whether your mouse pointer position is inside the rectangle, that's it.

See the code snippet:

QStyleOptionSpinBox opt;
this->initStyleOption(&opt);
QRect rect = this->style()->subControlRect(QStyle::CC_SpinBox,&opt,QStyle::SC_SpinBoxUp);
if(rect.contains(event->pos()))
qDebug() << "UP";
else
qDebug() << "DOWN";

Cheers

M.

GoranShekerov
18th May 2015, 00:22
mac_loo (Y) Thank you