Вопрос

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:

};
Это было полезно?

Решение

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.).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top