PDA

View Full Version : Connect the QGrapchisScene of QGraphicsView click signal



aguayro
4th February 2013, 20:01
I have a QGraphicsView and want to detect user clicks, but i have a problem, clickng the scrollbars also emit the click signal. How could i avoid the signal when scrollbars are clicked?

thsi is my code:



rs_Graphicsview::rs_Graphicsview(QWidget *parent) :
QGraphicsView(parent)
{
installEventFilter(this);
}


bool rs_Graphicsview::eventFilter(QObject *obj, QEvent *evnt) {
if (evnt->type() == QEvent::MouseButtonPress) {
emit clicked();

}
return QObject::eventFilter(obj, evnt);
}

wysota
4th February 2013, 20:08
Install the event filter on the view's viewport().

Santosh Reddy
4th February 2013, 20:13
What do want to achieve my getting user click on the QGraphicsView?

Install the event filter on the viewport


installEventFilter(this->viewport());


Most of the cases it is more useful to receive (filter) on the QGraphicsScene?

Edit: I should improve my keyboard skills :) (it took ~10 min to type this :confused:)

aguayro
5th February 2013, 01:05
Using

installEventFilter(this->viewport());
desn't work.

Just stops emitting clicked() signal.

Santosh Reddy
5th February 2013, 06:53
try this


bool rs_Graphicsview::eventFilter(QObject *obj, QEvent *evnt) {
if (evnt->type() == QEvent::MouseButtonPress) {
emit clicked();

}
//return QObject::eventFilter(obj, evnt);
return false;
}

aguayro
5th February 2013, 13:03
It's strange, just does the opposite, works on scrollbar clicks, and ignores scene area clicks :o

wysota
5th February 2013, 13:14
That's because you didn't install the filter on the correct object.

It should be:

view->viewport()->installEventFilter(this);

aguayro
5th February 2013, 13:38
still emitting when scrolbar clicked, i must be clumsy, seems easy but something fails

wysota
5th February 2013, 13:43
Well... it works just fine for me...


#include <QtGui>

class Monitor : public QObject {
public:
Monitor(QObject *parent = 0) : QObject(parent) {}

protected:
bool eventFilter(QObject *o, QEvent *e) {
if(e->type() == QEvent::MouseButtonPress) {
qDebug() << "CLICK";
}
return false;
}
};

int main(int argc, char **argv) {
QApplication app(argc, argv);
QGraphicsView view;
QGraphicsScene scene(0,0,2000,2000);
view.setScene(&scene);
Monitor monitor;
view.viewport()->installEventFilter(&monitor);
view.show();
return app.exec();
}

Santosh Reddy
5th February 2013, 15:31
Yes there is mistake in my post