QT中使用Event Filter监听按钮事件,Release后按钮不见

来源:互联网 发布:mac可玩的网游 编辑:程序博客网 时间:2024/06/05 19:37

问题RT,在程序中我使用了QT的监听事件,监听鼠标的位置,如果鼠标在按钮上就改变按钮的ICON,但是在Release版本中(Debug版本没问题),这些被监听的按钮都看不见了,于是开始了寻找答案的道路。

闲话少叙,直接上码:

void MainHomeForm::init(){    //为按钮注册事件    ui->SystemSetButton->installEventFilter(this);    ui->ZoneSetButton->installEventFilter(this);}
//系统监听器监听按钮对象bool MainHomeForm::eventFilter(QObject *target, QEvent *e){    if(target == ui->SystemSetButton)    {        if(e->type() == QEvent::Enter)        {            ui->SystemSetButton->resize(163,91);            ui->SystemSetButton->setIcon(QIcon(":/new/prefix1/back/系统设置2.png"));        }        else if(e->type() == QEvent::Leave)        {            ui->SystemSetButton->resize(115,60);            ui->SystemSetButton->setIcon(QIcon(":/new/prefix1/back/系统设置.png"));        }    }    else if(target == ui->ZoneSetButton)    {        if(e->type() == QEvent::Enter)        {            ui->ZoneSetButton->resize(163,91);            ui->ZoneSetButton->setIcon(QIcon(":/new/prefix1/back/区域控制2.png"));        }        else if(e->type() == QEvent::Leave)        {            ui->ZoneSetButton->resize(115,60);            ui->ZoneSetButton->setIcon(QIcon(":/new/prefix1/back/区域控制.png"));        }    }}

想法一:显示不了按钮图片,应该是图片的路径问题吧,于是使用在Release路径下添加imageformats文件夹,还是不行

想法二:我发现按钮都点不了了,那应该不是图片的路径问题了,直接出在按钮上,果断注释了为按钮注册事件的两句话,发现Release后按钮显示了

发现程序中我忽略了这么一个Warning:


哦,之前一直忽略了,原来是因为eventFilter这个函数我没给返回值,那我给一个好了

返回FALSE吧,发现按钮还是不见

返回TRUE吧,发现按钮可见了

预测:应该是返回值影响了按钮是否可见吧


最终解决办法:EventFilter要返回True哦

2 0
原创粉丝点击