Hello. Thank you very much for helping me find "QWidgetWindow".

Originally Posted by
high_flyer
And again, the problem with the QWidgetWindow is a good example why using the string based polling of classes or object names is bad practice and very fragile code.
And because its part of the private Qt area also means it can change, be renames or removed in any future Qt release breaking your code.
I absolutely agree with you. Performing a check on the name of the class is not correct.
To better reflect my actions, I threw out unnecessary code from the project.
Consequently, the project contains two classes: first.cpp and second.cpp.
In the first.cpp class, is created an object of the QPushButton class "Button1".
In the second.cpp class, is created an object of the QPushButton class "Button2".
In the second.cpp class is installed events filter for qApp.
In the first.cpp class is created the object of second.cpp "second1".
Graphically I depicted it in the picture below:
Forum_question_2_4.jpg
main.cpp
#include "first.h"
#include <QApplication>
int main(int argc, char *argv[])
{
first first1;
first1.show();
return a.exec();
}
#include "first.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
first first1;
first1.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
first.cpp
#include "first.h"
#include "second.h"
#include <QtWidgets>
{
this->resize(230, 70);
second *second1 = new second(this);
button1->setParent(this);
button1->setText("Button1");
button1->setObjectName("Button1");
button1->setGeometry(120, 10, 100, 25);
}
first::~first()
{
}
#include "first.h"
#include "second.h"
#include <QtWidgets>
first::first(QWidget *parent) : QMainWindow(parent)
{
this->resize(230, 70);
second *second1 = new second(this);
QPushButton *button1 = new QPushButton;
button1->setParent(this);
button1->setText("Button1");
button1->setObjectName("Button1");
button1->setGeometry(120, 10, 100, 25);
}
first::~first()
{
}
To copy to clipboard, switch view to plain text mode
second.cpp
#include "second.h"
#include <QtWidgets>
{
resize(115, 45);
qApp->installEventFilter(this);
button2->setParent(this);
button2->setText("Button2");
button2->setObjectName("Button2");
button2->setGeometry(10, 10, 100, 25);
}
second::~second(){
}
if (event
->type
() == QEvent::MouseButtonRelease) {
cc++;
qDebug() << cc;
qDebug() << watched->metaObject()->className() << endl;
return false;
}else{
return false;
}
}
#include "second.h"
#include <QtWidgets>
second::second(QWidget *parent) : QWidget(parent)
{
resize(115, 45);
qApp->installEventFilter(this);
QPushButton *button2 = new QPushButton;
button2->setParent(this);
button2->setText("Button2");
button2->setObjectName("Button2");
button2->setGeometry(10, 10, 100, 25);
}
second::~second(){
}
bool second::eventFilter(QObject *watched, QEvent *event){
if (event->type() == QEvent::MouseButtonRelease) {
QMouseEvent *pe = static_cast<QMouseEvent*>(event);
cc++;
qDebug() << cc;
qDebug() << watched->metaObject()->className() << endl;
return false;
}else{
return false;
}
}
To copy to clipboard, switch view to plain text mode
Now when I'm clicking a mouse on any objects, i have result:
Forum_question_2_5_1.jpg
Now I installed event filter for "first1" (first.cpp).
{
resize(115, 45);
parent->installEventFilter(this);
button2->setParent(this);
button2->setText("Button2");
button2->setObjectName("Button2");
button2->setGeometry(10, 10, 100, 25);
}
second::second(QWidget *parent) : QWidget(parent)
{
resize(115, 45);
parent->installEventFilter(this);
QPushButton *button2 = new QPushButton;
button2->setParent(this);
button2->setText("Button2");
button2->setObjectName("Button2");
button2->setGeometry(10, 10, 100, 25);
}
To copy to clipboard, switch view to plain text mode
"parent" is pointer to "first1" object.
When I'm clicking a mouse on "first1" or "second1" i have result:
Forum_question_2_5_2.png
When I'm clicking a mouse on any buttons (button1 or button2) i dont have result. This is probably due to the fact that eventFilter does not work for them.
This is why I want to leave eventFilter for qApp and not for other objects. I want so that eventFilter work globally for all objects.
How to achieve this by setting eventFilter to the first.cpp class object ("first1")?
Bookmarks