PDA

View Full Version : QDialog with Qgraphicsview mouse event scroll bar zoom problem



rno
26th July 2011, 09:17
Hi,

I have designed a dialog containing a qgraphicsview using designer/creator with nodrag mode selected.

I have reimplemented the mouse event to catch left button down drag and mousewheel for zooming.

The drag works but when I use the mouse wheel, the zoom does not quite work if there is a vertical scroll bar. The vertical scroll bar catch the mousewheel event first and only when the scroll bar is at its upper or lower limits the zoom works (the scroll bar is reset to its middle position and it is the same problem all over again).

I have read few forum posts and I found the same problem posted before without any solution (or not clear to me):

http://www.qtcentre.org/threads/29173-Questions-about-zooming-QGraphicsView?highlight=mouse+qgraphicsview

thanks if you can help,
Rno

TheExtraPiece
3rd August 2011, 01:14
I just finished fixing this exact problem, so perhaps I can be of some help. What I did was create a new class with an event filter, and then installed that filter on the QGraphicsView from my .ui file.
It is important that you install the event filter on the viewport of the QGraphicsView, otherwise it won't work. Here is the code that I used, I can give more help if you find that you need it.



#ifndef EVENTFILTERIZER_H
#define EVENTFILTERIZER_H

#include <QObject>
#include <QEvent>
#include <QWheelEvent>

class EventFilterizer : public QObject
{
Q_OBJECT
public:
explicit EventFilterizer(QObject *parent = 0);

protected:
bool eventFilter(QObject *obj, QEvent *event);

signals:
void scrollIntercept(int);

public slots:

};

#endif // EVENTFILTERIZER_H



#include "eventfilterizer.h"

EventFilterizer::EventFilterizer(QObject *parent) :
QObject(parent)
{
}

bool EventFilterizer::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::Wheel)
{
//code to activate your zoom function, however you choose to do it
//I used a signal to activate mine
return true;
}
else
{
// standard event processing
return QObject::eventFilter(obj, event);
}
}





//this goes in the class that the graphicsview is a part of
EventFilterizer* interceptor = new EventFilterizer;
ui->gcodePlotter->viewport()->installEventFilter(interceptor);