PDA

View Full Version : update window size



eric
8th January 2008, 17:22
Hello!
I want the size of my main window to change as the program runs.
This is my main function



int main(int argc, char *argv[])
{
QApplication application(argc, argv);
MyWidget window;

int x = 100; int y =100;
QRect screen = qApp->desktop()->screenGeometry();
window.setGeometry(5,20,screen.width()-x,screen.height()-y);

window.show();
return application.exec();
}


This above positions my window where I want it. But as the program runs the x and y variables will change and I'd like to resize window by doing

window.setGeometry(5,20,screen.width()-x,screen.height()-y);
from other functions. I cannot do this since "window" is not reconized by other functions.
I don't think I can put this implementation
MyWidget window; in my *.h file to make it public since that wouldn't work.
Does anyone have suggestions how to make this automatic window resizing work so that it could be done from functions other than "main". Maybe use updateGeometry() somehow?

high_flyer
8th January 2008, 22:08
Hmm.. ok I'll just answer your question:
One easy way would be to create a slot in MyWidget, and emit signals with the new hight and width from where ever it is you want to resize it from.

Something like:


void MyWidget::resize(int w, int h) //defined as public slot
{
QWidget::resize(w,h);
}


But there are other ways to go about it, depends on your case.

eric
13th January 2008, 16:52
Thank you, this a great solultion to resizing a window.

I only know how to emit signals from when user presses a button or does something.
For example:


connect(Button, SIGNAL(clicked()), this, SLOT(calculate()));

How do you make the program to emit a signal when program reaches a certain point?


My other question is this: How can I make the main window globally accessible for any function to resize it? Right now my main window is declared as:


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

If I try to do somethig like


window.resize();

from any function, it won't work because window is not declared in the *.h file or the MyWidget contructor. I don't think I can declare "window" in those files? Can I?

high_flyer
14th January 2008, 10:21
How do you make the program to emit a signal when program reaches a certain point?
You should really go through the documentation (http://doc.trolltech.com/4.3/signalsandslots.html), its one of the best there is, and its all in there.

But in a but shell:
when you want to emit a signal you do:

emit mySignal();
And make sure it is defined in the header file of your class.


My other question is this: How can I make the main window globally accessible for any function to resize it?
You don't need that, this is OOP, C++. no globals, unless REALLY needed!
You do it either with parameter, member, or, signal slots, where signal slots I think should be the first thing to try.