I’m studying the Qt4 library and I want to add some functionality to all the children of QWidget in my project. I need all widgets to have mousePressEvent overridden. Obviously I do not need to override it in every particular widget I use(I use many of them and I they to follow DRY rule) , instead I create a Foo class:
class Foo : public QWidget
{
Q_OBJECT
protected:
/* implementation is just a test */
virtual void mousePressEvent(QMouseEvent*) { this->move(0,0); }
};
And derive my button from it:
class FooButton : public QPushButton , public Foo
{
public:
FooButton (QWidget* parent) : QPushButton(parent) { };
};
But the event seems to be not overridden… What am I doing wrong?
Thanks in advance.
You are inheriting twice now from QWidget. This is problematic (see diamond problem).
Instead, drop your
Fooclass and move your custom mouse press event handler to yourFooButtonclass:If this doesn’t suit the design of your application, then try using virtual inheritance instead. That is, change
Footo:If that doesn’t help, you should follow the advice of the other answers and install an event handler.
(You can read about virtual inheritance in its Wikipedia article.)