Qt code inspired by http://codepen.io/stuffit/pen/KrAwx