Hmm, weird. I install event filter only in one place of code and only for one object.
Be kind, take a look at the code of my project, and tell me where I was mistaken?
Headers:
first.h
#ifndef FIRST_H
#define FIRST_H
#include <QMainWindow>
{
Q_OBJECT
public:
~first();
};
#endif // FIRST_H
#ifndef FIRST_H
#define FIRST_H
#include <QMainWindow>
class first : public QMainWindow
{
Q_OBJECT
public:
first(QWidget *parent = 0);
~first();
};
#endif // FIRST_H
To copy to clipboard, switch view to plain text mode
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
{
Q_OBJECT
public:
explicit second
(QWidget *parent
= nullptr
);
~second();
int cc = 0;
signals:
public slots:
};
#endif // SECOND_H
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
class second : public QWidget
{
Q_OBJECT
public:
explicit second(QWidget *parent = nullptr);
~second();
int cc = 0;
virtual bool eventFilter(QObject *watched, QEvent *event);
signals:
public slots:
};
#endif // SECOND_H
To copy to clipboard, switch view to plain text mode
Sources:
main.cpp
#include "first.h"
#include <QApplication>
int main(int argc, char *argv[])
{
first w;
w.show();
return a.exec();
}
#include "first.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
first w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
first.cpp
#include "first.h"
#include "second.h"
#include <QtWidgets>
{
second *second1 = new second(this);
}
first::~first()
{
}
#include "first.h"
#include "second.h"
#include <QtWidgets>
first::first(QWidget *parent) : QMainWindow(parent)
{
second *second1 = new second(this);
}
first::~first()
{
}
To copy to clipboard, switch view to plain text mode
second.cpp
#include "second.h"
#include <QtWidgets>
{
qApp->installEventFilter(this);
}
second::~second(){
}
if (event
->type
() == QEvent::MouseButtonRelease) {
cc++;
qDebug() << cc;
return false;
}else{
return false;
}
}
#include "second.h"
#include <QtWidgets>
second::second(QWidget *parent) : QWidget(parent)
{
qApp->installEventFilter(this);
}
second::~second(){
}
bool second::eventFilter(QObject *watched, QEvent *event){
if (event->type() == QEvent::MouseButtonRelease) {
cc++;
qDebug() << cc;
return false;
}else{
return false;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks