Important: Please read the Qt Code of Conduct -


  • hi i am programming Qt in visual studio and i have a problem with this code
    @#ifndef MYCLASS_H
    #define MYCLASS_H

    #include <QtGui/QMainWindow>
    #include "ui_myclass.h"

    class myclass : public QMainWindow

    myclass(QWidget *parent = 0, Qt::WFlags flags = 0);

    Ui::MyClassClass ui;
    bool eventFilter(QObject *, QEvent *);

    #endif // myclass_H
    @#include "myclass.h"
    #include <QMouseEvent>
    #include <QMessageBox>
    myclass::myclass(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)



    bool myclass::eventFilter( QObject *target, QEvent *eve )
    QMouseEvent *MouseEvent = static_cast < QMouseEvent *>( eve);
    if (MouseEvent->buttons()==Qt::LeftButton)
    return true;
    return myclass::eventFilter ( target , eve );

    my program breaks when it reach's to eventFilter
    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.

  • can somebody explain more i need to make click event for label

  • what more?

    PS: You should be subclassing QLabel and reimplementing the according mouse events. Installing an event filter for that is hacky.

  • i dont understand the reason and i want to know how should i make a click filter on label without creating new class

  • 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.

  • thank you very much i dont know why i did such a beginner mistake thanks

Log in to reply