【问题标题】:event(QEvent*) conflicts with mousePressEvent(QMouseEvent *)?事件(QEvent*)与 mousePressEvent(QMouseEvent *)冲突?
【发布时间】:2012-09-30 05:38:08
【问题描述】:

在 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:

};

【问题讨论】:

    标签: qt button qevent mousepress


    【解决方案1】:

    小部件接收到的所有事件都通过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(..) 等)分发。

    【讨论】:

    • 坦克!那么我怎样才能使'mousePressEvent'的功能起作用?或达到相同的效果?(我想首先显示一个图标和一些文本(外观)QToolButton,但我不想要QToolButton的其他行为。后来我想要获取'mousePress'事件并做一些事情(那是:当鼠标点击按钮时,我有处理事件的函数))
    猜你喜欢
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 2020-01-13
    • 1970-01-01
    相关资源
    最近更新 更多