문제

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