Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

why QCombobox receive mousePressEvent when user click on other window?



  • when user click outside QCombobox, the QComboBoxPrivateContainer::mousePressEvent will be trigger, then the hidepopup() function will be call. how can i implement this behavior like qt qcombobox.cpp.
    i write a own QWidget subclass,but i dont know how to close the popup-listview when user click other space in the window.

    void QComboBoxPrivateContainer::mousePressEvent(QMouseEvent *e){
    
        QStyleOptionComboBox opt = comboStyleOption();
        opt.subControls = QStyle::SC_All;
        opt.activeSubControls = QStyle::SC_ComboBoxArrow;
        QStyle::SubControl sc = combo->style()->hitTestComplexControl(QStyle::CC_ComboBox, &opt,
                                                               combo->mapFromGlobal(e->globalPos()),
                                                               combo);
        if ((combo->isEditable() && sc == QStyle::SC_ComboBoxArrow)
            || (!combo->isEditable() && sc != QStyle::SC_None))
            setAttribute(Qt::WA_NoMouseReplay);
        combo->hidePopup();
    }
    


  • As doc says in void QWidget::mousePressEvent(QMouseEvent *event)

    The default implementation implements the closing of popup widgets when you click outside the window. For other widget types it does nothing.

    That means a popup window (Qt::Popup) will receive mousePressEvent when clicking outside it (to close it).



  • As doc says in void QWidget::mousePressEvent(QMouseEvent *event)

    The default implementation implements the closing of popup widgets when you click outside the window. For other widget types it does nothing.

    That means a popup window (Qt::Popup) will receive mousePressEvent when clicking outside it (to close it).



  • @Bonnie thankyou. I need to learn about window with Qt::popup.


Log in to reply