PDA

View Full Version : problem in eventFilter of QGraphicsView()



wagmare
18th May 2009, 17:17
hi friends,
in QGraphicsView i am try to install event filter to an item .. to capture a key ...


RecRepView::RecRepView(QWidget *parent)
: QGraphicsView(parent)
{
QRectF bounds(0, -20, 700, 500);
scene = new QGraphicsScene(bounds, this);
setScene(scene);
//this is the QGraphicsItem
logout = new BackItem(QRectF(0, 0, 70, 65));
logout->setPos(20, 405);
logout->setFlag(QGraphicsItem::ItemIsSelectable);
logout->installEventFilter(this);
connect(logout, SIGNAL(closeSignal()), qApp, SLOT(quit()));
scene->addItem(logout);
}


and in event filter


bool RecRepView:: eventFilter(QObject *ob, QEvent *e)
{
printf("is it coming inside ..\n");
if(ob == logout && e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
if(ke->key()==Qt::Key_F1){
some cond .....
}
return true;
}
return QWidget::eventFilter(ob, e);
}


but even the printf output is not displaying .....
i dont know where i done the mistake ... please help ....

Lykurg
18th May 2009, 17:51
Install the filter on the scene or view, not on the item.

this->installEventFilter(this);
//...
return QGraphicsView::eventFilter(ob, e);

wagmare
19th May 2009, 06:27
Install the filter on the scene or view, not on the item.

this->installEventFilter(this);
//...
return QGraphicsView::eventFilter(ob, e);


of course i try that one too ....

my full code


RecRepView::RecRepView(QWidget *parent)
: QGraphicsView(parent)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
setFrameShape(QFrame::NoFrame);
setCacheMode(CacheBackground);
setViewportUpdateMode(FullViewportUpdate);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
setStyleSheet("background:#00002E");
QRectF bounds(0, -20, 700, 500);
scene = new QGraphicsScene(bounds, this);
setScene(scene);
this->installEventFilter(this);
signalMapper = new QSignalMapper();
int i =0;
mapValue[i] = STARTVALUE;
button1 = new GraphButton(QRectF(0,0, 150, 40), 1);
button1->setFlag(QGraphicsItem::ItemIsSelectable);
button1->setPos(475, 330);
scene->addItem(button1);
connect(button1, SIGNAL(activated()), signalMapper, SLOT(map()));
signalMapper->setMapping(button1,mapValue[i]);
i++;


mapValue[i] = STOPVALUE;
button2 = new GraphButton(QRectF(0,0, 150, 40), 2);
button2->setFlag(QGraphicsItem::ItemIsSelectable);
button2->setPos(475, 400);
scene->addItem(button2);
connect(button2, SIGNAL(activated()), signalMapper, SLOT(map()));
signalMapper->setMapping(button2,mapValue[i]);

logout = new BackItem(QRectF(0, 0, 70, 65));
logout->setPos(20, 405);
logout->setFlag(QGraphicsItem::ItemIsSelectable);
// log connect(logout, SIGNAL(closeSignal()), qApp, SLOT(quit()));
scene->addItem(logout);

connect(signalMapper, SIGNAL(mapped(int )), this, SIGNAL(activated(int)));

connect(this, SIGNAL(activated(int )), this, SLOT(recRepSlot(int )));

}

void RecRepView ::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
}

void RecRepView::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_UNUSED(rect);

QRectF rects(450,310, 200, 160);
QPen pen(QColor(211, 210, 212), 0.5);
painter->setBrush(QColor(128, 128, 128));
painter->setPen(pen);
painter->drawRect(rects);

}bool RecRepView:: eventFilter(QObject *ob, QEvent *e)
{
if(ob == logout && e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
if(ke->key()==Qt::Key_F1){
printf("is it coming inside ..\n");

}
return true;
}
return QWidget::eventFilter(ob, e);
}
void RecRepView::recRepSlot(int value)
{
printf("the value coming inside :%d \n", value);

}





GraphButton::GraphButton(const QRectF &rect, int itemNo)
: QGraphicsRectItem(rect)
{
}

void GraphButton::mousePressEvent(QGraphicsSceneMouseEv ent *event)
{
if(event->button() != Qt::LeftButton)
{
QGraphicsRectItem::mousePressEvent(event);
return;
}
emit activated();
}



please help ....

wysota
19th May 2009, 09:26
What is the point of installing an event filter on "this"? Why do you need the event filter in the first place? Can't you use the regular means of handling events?

wagmare
19th May 2009, 10:45
when the user press key <F1> .. i have to close the application or else user can click logout item .. like wise there are seven buttons <F2>, <F3> ,<F4> ... , each having different functions ... so i am planning to catch the event of pressing F1 ,... and try to do the option ....
why this one is not working ..? shall i use QAction instead for this ..?

Lykurg
19th May 2009, 13:45
when the user press key <F1> .. i have to close the application or else user can click logout item .. like wise there are seven buttons <F2>, <F3> ,<F4> ... , each having different functions ... so i am planning to catch the event of pressing F1 ,... and try to do the option ....
why this one is not working ..? shall i use QAction instead for this ..?

well the item has to be focused to get the event - I guess. If you whant a global short cut - seems to me you whant one - use a QAction with QAction::setShortcutContext(Qt::ApplicationShortcu t).

wysota
19th May 2009, 14:33
At "worst" you can reimplement QGraphicsView::keyPressEvent(). Event filters are for something else.

wagmare
20th July 2009, 13:28
we have to install event filter on QGraphicsScene() not in QGraphicsView() itself ..

scene->installEventFilter(this);

now the above code working perfectly .