PDA

View Full Version : access main window from function



eric
17th January 2008, 22:20
Hello!

How do you access the main window (that's declared only in main function) from other functions of the same class?
This below is my main() and I can resize the window as shown.


int main(int argc, char *argv[])
{
QApplication application(argc, argv);
MyWidget window;
window.resize(1200,800);
window.show();
return application.exec();
}


But why can't I do it from another function like


void MyWidget::reSize()
{
window.resize(1200,800);
}


The comiler complains like this: "insufficient contextual information to determine type". I think I need to declare "window" somewhere else to make this work.
I do not think that I can declare "window" in my *.h file, can I? Isn't this instance a bit different from all others?

jacek
17th January 2008, 22:32
MyWidget::reSize() method should resize "this" widget --- you don't need access to window variable:

void MyWidget::reSize()
{
resize(1200,800);
}

eric
17th January 2008, 22:46
Indeed, this works. Thank you.
One thing I don't understand is if you replace integers with variables and execute it several times, why does the command still execute only once. For example consider this:


for(int x=0; x<100; x++)
{
resize(100-x, 100-x);
}


Why doesn't this automatically shrink the window. I'm probably asking something very stupid.

jacek
17th January 2008, 23:14
You can't shrink the window beyond it's minimal size. If you have set a top-level layout for MyWidget, then the minimal size will be calculated so that all child widgets can fit into your widget.

And you won't see the animation, because the program flow must return to the event loop before resize() will work. The best approach here would be to use QTimer.

eric
17th January 2008, 23:23
Actually the example I gave was just a theoretical example.
What I have in my program is this:



//step1: calculate x and y (both numbers are OK for window size)

//step2:
resize(x,y);
QApplication::processEvents();

//step3: do graphics and return to step 1

steps 1 and 2 are in the same function, step 3 is in a different function.

Why does this code resize the screen only the first time it passes through it? Only the first set of x and y are executed and all the following ones are ignored.
I am doing all this to make the window wrap around a photo as I open it on the screen. Different photos are different size and I want the window size readjusted for each photo.

I use a lot of addStretch() to make my layout. Could this be causing the problem? Maybe once a large window is made the resize function cannot shrink the window because it cannot fit the expanded stretch in the new window? I don't know if this is making sense.

eric
18th January 2008, 21:33
So, how do you refresh


resize(int, int);

when performed from another function than main?

For some reason I can only execute this command once, there's no additional effect when I loop back to it with new integers.
Is "resize()" a good command to use for readjusting the window size during the program execution?

jacek
19th January 2008, 22:29
Can't you simply use QWidget::adjustSize()?