Fix #895 by adapting to Qt5 and cleaning up properly