QEvent
-
hi i am programming Qt in visual studio and i have a problem with this code
myclass.h
@#ifndef MYCLASS_H
#define MYCLASS_H#include <QtGui/QMainWindow>
#include "ui_myclass.h"class myclass : public QMainWindow
{
Q_OBJECTpublic:
myclass(QWidget *parent = 0, Qt::WFlags flags = 0);
~myclass();private:
Ui::MyClassClass ui;
protected:
bool eventFilter(QObject *, QEvent *);
};#endif // myclass_H
@
myclass.cpp
@#include "myclass.h"
#include <QMouseEvent>
#include <QMessageBox>
myclass::myclass(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
ui.label->installEventFilter(this);
}myclass::~myclass()
{}
bool myclass::eventFilter( QObject *target, QEvent *eve )
{
QMouseEvent *MouseEvent = static_cast < QMouseEvent *>( eve);
if (MouseEvent->buttons()==Qt::LeftButton)
{
QMessageBox::information(this,"Click","");
return true;
}
return myclass::eventFilter ( target , eve );
}
@my program breaks when it reach's to eventFilter
!http://dl.2rialy.ir/ZiDoM-Ups/91/eror.JPG(error)!
what should i do this code works in qtcreator but i want in visual studio -
Infinite recursion in eventFilter function. You should call the base class version of the function if you want to inherit functionality in reimplemented virtual functions. Currently you're just doing recursion without break condition, of course leading to a stack overflow.
-
In your "myclass::eventFilter ( target , eve );" function you call "myclass::eventFilter ( target , eve );" again (in the last line as return value). So you call the function itself again. And that calls itself again, and itself again, and itself again, and itself again,... stack overflow.
You probably wanted to call "return [baseclass]::eventFilter ( target , eve );" where [baseclass] is the base class of the respective derived class. In this case QMainWindow.