Key role of polar nanoregions in the cubic-to-tetragonal phase transition of potassium-based perovskites