If I were you, I'd check if ui->view->width() and ui->view->height() return values you expect since if you call this code before the window is first shown, they are likely to have bogus values (as of course explained in the docs).