Results 1 to 7 of 7

Thread: access main window from function

  1. #1
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication application(argc, argv);
    4. MyWidget window;
    5. window.resize(1200,800);
    6. window.show();
    7. return application.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    But why can't I do it from another function like
    Qt Code:
    1. void MyWidget::reSize()
    2. {
    3. window.resize(1200,800);
    4. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: access main window from function

    MyWidget::reSize() method should resize "this" widget --- you don't need access to window variable:
    Qt Code:
    1. void MyWidget::reSize()
    2. {
    3. resize(1200,800);
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to jacek for this useful post:

    eric (17th January 2008)

  4. #3
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. for(int x=0; x<100; x++)
    2. {
    3. resize(100-x, 100-x);
    4. }
    To copy to clipboard, switch view to plain text mode 

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

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  6. The following user says thank you to jacek for this useful post:

    eric (17th January 2008)

  7. #5
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: access main window from function

    Actually the example I gave was just a theoretical example.
    What I have in my program is this:

    Qt Code:
    1. //step1: calculate x and y (both numbers are OK for window size)
    2.  
    3. //step2:
    4. resize(x,y);
    5. QApplication::processEvents();
    6.  
    7. //step3: do graphics and return to step 1
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by eric; 17th January 2008 at 23:31.

  8. #6
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: access main window from function

    So, how do you refresh
    Qt Code:
    1. resize(int, int);
    To copy to clipboard, switch view to plain text mode 
    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?

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: access main window from function

    Can't you simply use QWidget::adjustSize()?

Similar Threads

  1. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 13:57
  2. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 18:33
  3. QDialog - Calling Main Window Function
    By Preeteesh in forum Newbie
    Replies: 4
    Last Post: 20th June 2007, 19:41
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.