Qt自定义控件的事件,使用重写事件或事件过滤器eventFilter
作者:互联网
方法1:重写自定义控件的mousePressEvent方法。
protected:
void mousePressEvent(QMouseEvent *);
//点击自定义控件
void WidgetPayItem::mousePressEvent(QMouseEvent * e)
{
qInfo()<<"点击自定义控件";
}
方法2:注册事件过滤器eventFilter
protected:
bool eventFilter(QObject *obj, QEvent *e);
//监视对象的事件
bool WidgetPay::eventFilter(QObject *obj, QEvent *e)
{
if (obj == m_widgetPayItem1)
{
if (e->type() == QEvent::MouseButtonPress){
qInfo()<<"点击自定义控件1";
}
}
else if (obj == m_widgetPayItem2)
{
if (e->type() == QEvent::MouseButtonPress){
qInfo()<<"点击自定义控件2";
}
}
else if (obj == m_widgetPayItem3)
{
if (e->type() == QEvent::MouseButtonPress){
qInfo()<<"点击自定义控件3";
}
}
return QWidget::eventFilter(obj,e);
}
//注册事件过滤器
m_widgetPayItem1->installEventFilter(this);
标签:控件,eventFilter,自定义,事件,qInfo,QEvent,mousePressEvent 来源: https://blog.csdn.net/xuefu2008/article/details/120484088