I think this can be simplified. You already have a check for m_displayIds.contains(path) further down, and another m_displayIds.indexOf(path) as well. disconnect() won't be necessary if the display object is deleted, which is what happens when you erase(path) it. So all that's left is:
- For the
remove/eraseblock, putif (auto index = m_displayIds.indexOf(path); index != -1)instead ofif (m_displayIds.contains(path)) - After
remove/erasestatements, add the extra block containing theconnect()statement with a combined conditionif (index == 0 && !m_displayIds.Empty()).