See https://github.com/qgis/QGIS-Enhancement-Proposals/issues/302 where there is discussion about functionality like the one you have in your plugin...