1

QT: QToolButton から継承されたクラスを使用して event(QEvent*) を書き換えます。「mousePressEvent」を追加したいのですが、ヒットしません。event(QEvent*) は mousePressEvent(QMouseEvent *) と競合しますか? ありがとうございました。

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
}

クラスは次のとおりです。

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

public slots:

};
4

1 に答える 1

2

ウィジェットによって受信されたすべてのイベントは通過しevent(..)、適切なイベントハンドラーメソッドにリダイレクトされます。マウスプレスイベント処理を追加したいだけの場合は、ペイントイベント以外のイベントを転送しないという間違いを犯しました。これを実行します。

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

また、イベントはイベントキュー(など)protectedを介してのみ配信されることになっているため、イベントハンドラメソッドは実際にに含まれている必要があります。QCoreApplication::postEvent(..)

于 2012-09-30T07:02:59.813 に答える