1

In QT: I use a class inherited from QToolButton and rewrite event(QEvent*), now I want to add 'mousePressEvent', but it never gets hit, does event(QEvent*) conflict with mousePressEvent(QMouseEvent *) ? Thank you.

bool IconLabel::event (QEvent* e ) {
   if ( e->type() == QEvent::Paint) {
      return QToolButton::event(e);

   }
   return true;
}
void IconLabel::mousePressEvent(QMouseEvent* e)
{
   int a = 1;//example
    a = 2;// example//Handle the event
}

The class is:

class IconLabel : public QToolButton
{
    Q_OBJECT
public:
    explicit IconLabel(QWidget *parent = 0);
    bool event (QEvent* e );
    void mousePressEvent(QMouseEvent* e);
signals:

public slots:

};
Al2O3
  • 3,103
  • 4
  • 26
  • 52

1 Answers1

2

All events received by a widget pass through event(..), and then are redirected to the appropriate event handler method. You have made the mistake of not forwarding on any events except paint events, if you just want to add mouse press event handling do this:

bool IconLabel::event (QEvent* e ) {
    if ( e->type() == QEvent::Paint ||
         e->type() == QEvent::QEvent::MouseButtonPress ) {
        return QToolButton::event(e);
    }
    return true;
}

Also event handler methods should really be in protected, because events are only supposed to be distributed via the event queue (QCoreApplication::postEvent(..), etc.).

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • Tanks! Then How can I make function of 'mousePressEvent' work? or achieve the same effect?(I want to show an Icon and some text (the appearance) of QToolButton at first, but I don't want other behavior of QToolButton. Later I want the get 'mousePress' event and do something(That is: when mouse clicks the button, I have a function to deal with the event)) – Al2O3 Sep 30 '12 at 08:02