Self-trapping triggered by losses in cavity QED