PDA

View Full Version : QEvent for QGraphicsView scrollbars?



forrestfsu
20th November 2006, 18:56
Is there a way to determine whether or not a mousepress release occurred on a scrollbar? It seems that QEvent::MouseButtonRelease only gets activated when the user releases focus within the graphics view, and not on the scrollbars...

e8johan
20th November 2006, 19:38
The graphics view inhert the abstract scroll area - that is where the scroll bars enter the picture and that is where you have to look for the events/signals that you are looking for.

e8johan
20th November 2006, 19:39
Ok, I meant to also tell you that you can get the actual QScrollBar widgets using the verticalScrollbar and horizontalScrollbar methods.

jpn
20th November 2006, 19:45
QScrollBar inherits QAbstractSlider, which offers signals:

QAbstractSlider::sliderPressed()
QAbstractSlider::sliderReleased()


You can access the scroll bars via:

QAbstractScrollArea::horizontalScrollBar()
QAbstractScrollArea::verticalScrollBar()

forrestfsu
20th November 2006, 20:36
Thanks for the tip guys. Is it possible to install an eventfilter on it so that I can access it similar to this:



bool theFormName::eventFilter(QObject *o, QEvent *e){
if (o == ui.graphicsView)
if (e->type() == QAbstractSlider::sliderReleased())
// do something
}

jpn
20th November 2006, 20:46
Those are signals so you would use them in the usual way:


connect(horizontalScrollBar(), SIGNAL(sliderPressed()), this, SLOT(doSomething()));
connect(horizontalScrollBar(), SIGNAL(sliderReleased()), this, SLOT(doSomething()));
// and so on...


If you want to use the event filtering approach instead, the corresponding types are QEvent::MouseButtonPress and QEvent::MouseButtonRelease.

forrestfsu
20th November 2006, 20:54
Thanks to both of you, your answers were right on target (as usual). :)

I was using an event filter for other portions of my code, and wanted to keep it consistent. But the solution you showed was simple enough...and I'm definitely better off using that rather then complicating the situation.

Bitto
20th November 2006, 23:32
For the record, then. If you reimplement the scrollContentsBy() function of QAbstractScrollArea, you will receive the delta values from both scroll bars at once, without having to declare slots or install event filters.



void MyCustomView::scrollContentsBy(int dx, int dy)
{
// dx = horizontal delta
// dy = vertical delta
}


Btw, installing an event filter:



MyCustomView::MyCustomView(QGraphicsScene *scene, QObject *parent)
: QGraphicsView(scene, parent)
{
horizontalScrollBar()->installEventFilter(this);
verticalScrollBar()->installEventFilter(this);
}

bool MyCustomView::eventFilter(QObject *watched, QEvent *event)
{
bool vbar = (object == verticalScrollBar();
bool hbar = (object == horizontalScrollBar();
if (!vbar && !hbar)
return false;

switch (event) {
case MouseButtonPress:
if (vbar) { /* someone pressed the vertical bar */ }
else { /* someone pressed the horizontal bar */ }
break;
case MouseButtonPress:
if (vbar) { /* someone released the vertical bar */ }
else { /* someone released the horizontal bar */ }
break;
case MouseMove:
if (vbar) { /* someone pressed and moved the vertical bar */ }
else { /* someone pressed and moved the vertical bar */ }
break;
...
default:
break;
}
}

forrestfsu
21st November 2006, 14:25
thanks for the eventfilter example. I noticed that the SliderReleased() signal only emits a signal for the scrollbar, not the "scroll arrow" buttons. By the name of the function, I guess that makes sense. I was wondering if there was something similar that I can call for the scroll arrow buttons. I've been digging through the QAbstractScrollArea reference, but I'm not sure if that's where I should be looking.

Bitto
2nd December 2006, 07:42
No, I'd go for the event filter in this case.