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

Closing the popup of the combobox with tree view



  • Hi,
    This is a follow-up post of the following
    https://forum.qt.io/topic/125326/presetting-a-combobox-to-one-of-its-tree-view-popup-entry
    With a tree view in the popup panel of the combobox one needs to reimplement showPopup and hidePopup. If not, it is not possible to select a child item because the popup is closing wenn you click on a parent item for openning the childs branch.

    void TreeBox::hidePopup()
    {
        if (m_hide==true)
        {
            QComboBox::hidePopup();
            m_hide = false;
        }
    }
    

    I also had to reimplement the eventFilter on this object

    bool TreeBox::eventFilter(QObject *watched, QEvent *event)
    {
        if (event->type() == QEvent::MouseButtonPress && watched == m_tree->viewport())
        {
            QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
            QModelIndex index = view()->indexAt(mouseEvent->pos());
    
            if (index.parent().row()==-1)
                m_hide = false;
            else
                m_hide = true;
       }
        return false;
    }
    

    Each time one clicks an item on the popup panel the hidePopup slot is called but I forward the signal only if the user has clicked on a child item.

    The issue I have now is that when I click outside the popup panel it will not close as it does on a regular Combobox.
    I checked that the hidePopup slot is called but because I didn't click on a child item m_hide is false and the panel is not closed.

    So my question is, how can I get the information that I clicked outside the popup panel?



  • Thank you,

    In fact adding your usggestion didn't work because a click outside the combobox didn't trigger the MouseButtonPressed.

    But your suggestion gave me the idea to check what kind of events were triggered when cliking outside the combobox.
    I noticed that there is a mouseLeave event so I modified the eventFilter slot as follows:

    bool TreeBox::eventFilter(QObject *watched, QEvent *event)
    {
    if (event->type() == QEvent::MouseButtonPress && watched == m_tree->viewport())
        {
            QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
            QModelIndex index = view()->indexAt(mouseEvent->pos());
    
            if (index.parent().row()==-1)
                m_hide = false;
            else
               m_hide = true;
          }
        else if (event->type() == QEvent::HoverLeave)
        {
             m_hide = true;
        }
    }
    

    Now it's working



  • if you click outside then the index should be invalid.
    Add something like this:

    if(!index.isValid()) {
        m_hide = true;
        return;
    }
    


  • Thank you,

    In fact adding your usggestion didn't work because a click outside the combobox didn't trigger the MouseButtonPressed.

    But your suggestion gave me the idea to check what kind of events were triggered when cliking outside the combobox.
    I noticed that there is a mouseLeave event so I modified the eventFilter slot as follows:

    bool TreeBox::eventFilter(QObject *watched, QEvent *event)
    {
    if (event->type() == QEvent::MouseButtonPress && watched == m_tree->viewport())
        {
            QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
            QModelIndex index = view()->indexAt(mouseEvent->pos());
    
            if (index.parent().row()==-1)
                m_hide = false;
            else
               m_hide = true;
          }
        else if (event->type() == QEvent::HoverLeave)
        {
             m_hide = true;
        }
    }
    

    Now it's working


Log in to reply