zl程序教程

您现在的位置是:首页 >  工具

当前栏目

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

Qt事件 监听 filter Event button Release 不见
2023-09-27 14:27:21 时间
版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/zhenyu5211314/article/details/27201043

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

闲话少叙,直接上码:

void MainHomeForm::init()
{
    //为button注冊事件
    ui->SystemSetButton->installEventFilter(this);
    ui->ZoneSetButton->installEventFilter(this);
}
//系统监听器监听button对象
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"));
        }
    }
}

想法一:显示不了button图片,应该是图片的路径问题吧,于是使用在Release路径下加入imageformats目录,还是不行

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

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


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

返回FALSE吧,发现button还是不见

返回TRUE吧,发现button可见了

预測:应该是返回值影响了button是否可见吧


终于解决的方法:EventFilter要返回True哦