PDA

View Full Version : graphics view FitInView problem



aamer4yu
24th January 2007, 11:16
I am submitting a scaled down code of my application which has the fit in view problem.

In CMainWindow constructor, I am calling FitInView() so that the contents of the view are fit to the viewport at the initial display. But this thing doesnt work. I checked the zoom level before and after the scene->views().first()->fitInView(rect,Qt::KeepAspectRatio);. They work correctly when FIV button is pressed from the main window, but not when FitInView is called from the CMainWindow constructor.

When I press the FIV button after the main window is displayed, the contents are properly fitted onto the viewport.

Why doesnt FitInView work properly in the constructor of the main window ?? Is it a bug or am I mising something ?? :confused:

jpn
24th January 2007, 11:26
The geometry of a QWidget is calculated when it's show for the first time (unless forcing a fixed size). So sounds like a problem of undefined geometries..

aamer4yu
24th January 2007, 11:35
So is there a way around to show the view fitted properly initially ??
Even setting the view->setMinimumSize(), view->setMaximumSize() doesnt help.

I had tried using a timer and it had worked. I used to set the timer to call FitInView, and after the first call, stop the timer.

Is this timer solution fine, or can there be a better way around ?

jpn
24th January 2007, 12:22
I had tried using a timer and it had worked. I used to set the timer to call FitInView, and after the first call, stop the timer.
You could also use QTimer::singleShot().


Is this timer solution fine, or can there be a better way around ?
I don't know about better, but just another option is to invoke fitInView() in the showEvent() of the main window, for example:


void MainWindow::showEvent(QShowEvent* event)
{
QMainWindow::showEvent(event);
static bool fitted = false;
if (!fitted)
{
// put the fitInView() here..
fitted = true;
}
}

aamer4yu
25th January 2007, 04:29
the showevent is also not helping :(

neways I did the workaround by the timer itself.... thx for the replies :)

aamer4yu
25th January 2007, 09:58
I found another solution... though it gives same result as of usiing timer...

in main function, call CMainWindow::FitInView() function, after mainWindow.showMaximised() call !! :)

mainWindow.showMaximized();
mainWindow.FitInView();


but i prefer using the timer one, as it helps to keep the initialising things in the constructor .

Gopala Krishna
25th January 2007, 10:24
Even I feel the timer approach is best. Actually I remember reading in KDE 3 development guide that most of the things regarding geometry of widget are better to be done in a slot and then using QTimer::singleShot().