用于QComboBox的鼠标按键插槽 - 如果用户单击组合框,则动态填充项目

时间:2015-11-18 06:39:55

标签: c++ qt qcombobox

在Qt QComboBox中,没有点击信号来获取要覆盖的插槽。但我需要动态填充组合框项目。也就是说,我需要检查项目并更新组合框的所有项目列表(当用户更改其他项目应该对列表产生影响时)。

1 个答案:

答案 0 :(得分:1)

对于这个复杂的问题,我们可以在qt中使用事件过滤器方法。尝试为所有操作返回 false 以进行进一步处理。

bool QtMyWindow::eventFilter(QObject *f_object, QEvent *f_event){
    if(f_object == ui->comboBoxResetValue){
        if(f_event->type() == QEvent::MouseButtonPress){
            fillItems(); // try to clear before fill to avoid repetitions
        }
        return false;
    }
    return false;
}

我们还告知对象我们将为您过滤事件,因此在构造函数中包含此行

QtMyWindow::QtMyWindow(QObject* parent,...)
{
    ...
    ui->comboBox->installEventFilter(this);
}