Stole some code form QLib