Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Closing the popup of the combobox with tree view

Closing the popup of the combobox with tree view

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 326 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bert59
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bert59
      wrote on last edited by
      #3

      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

      1 Reply Last reply
      2
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by gde23
        #2

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

        if(!index.isValid()) {
            m_hide = true;
            return;
        }
        
        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bert59
          wrote on last edited by
          #3

          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

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved