Mouse press slot for QComboBox - fill the items dynamically if user clicks the combobox

守給你的承諾、 提交于 2019-12-11 01:33:21

问题


In Qt QComboBox, there is no clicked signal to get the slot to override. But I need to fill the combo box items dynamically. That is, I need to check the items and renew the combo box all items list (when user changes other items should have impact on the list).


回答1:


For this complicated issue, we can use event filter method in qt. Try to return false for all action to make the further process taken place.

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;
}

and we also inform the object that we are going to filter the event for you, so in constructor, include this line

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


来源:https://stackoverflow.com/questions/33773368/mouse-press-slot-for-qcombobox-fill-the-items-dynamically-if-user-clicks-the-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!