access main window from function
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.
Code:
int main(int argc, char *argv[])
{
MyWidget window;
window.resize(1200,800);
window.show();
return application.exec();
}
But why can't I do it from another function like
Code:
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?
Re: access main window from function
MyWidget::reSize() method should resize "this" widget --- you don't need access to window variable:
Code:
void MyWidget::reSize()
{
resize(1200,800);
}
Re: access main window from function
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:
Code:
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.
Re: access main window from function
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.
Re: access main window from function
Actually the example I gave was just a theoretical example.
What I have in my program is this:
Code:
//step1: calculate x and y (both numbers are OK for window size)
//step2:
resize(x,y);
//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.
Re: access main window from function
So, how do you refresh
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?
Re: access main window from function
Can't you simply use QWidget::adjustSize()?