This function may already exist in `core/qtutils.h`. If not, please move it there. Also, `r` should be taken in by const reference (same thing for the other functions).