I have two classes:
typedef std::shared_ptr<AdaptedWidget> window_ptr;
class WindowManager
{
public:
...
private:
std::stack<window_ptr> m_windowsStack;
}
and
class AdaptedWidget: public QWidget
{
Q_OBJECT
public:
AdaptedWidget(AdaptedWidget *parent = 0);
bool event(QEvent *event);
};
bool AdaptedWidget::event(QEvent *event)
{
if (event->type() == QEvent::NonClientAreaMouseButtonPress ||
event->type() == QEvent::MouseButtonPress)
{
qDebug() << "mainwindwo press";
}
return QWidget::event(event);
}
I need to get information about events that happen in AdaptedWidget
objects from my WindowManager
object, How can I do that?
Event filters are the Qt way to accomplish your task.
Make your
WindowManager
class a subclass ofQObject
and provide an implementation for itseventFilter()
method.After that, every time you create an
AdaptedWidget
useinstallEventFilter()
to install yourWindowManager
instance as an event filter on it.and
and when creating each
AdaptedWidget
instance, install theWindowManager
as an event filter:The
AdaptedWidget
class should have a signal that indicates a mouse press, e.g.Another approach is to use event filters, but that unnecessarily tightly couples the two classes.