Results 1 to 4 of 4

Thread: update window size

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

    Default update window size

    Hello!
    I want the size of my main window to change as the program runs.
    This is my main function

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication application(argc, argv);
    4. MyWidget window;
    5.  
    6. int x = 100; int y =100;
    7. QRect screen = qApp->desktop()->screenGeometry();
    8. window.setGeometry(5,20,screen.width()-x,screen.height()-y);
    9.  
    10. window.show();
    11. return application.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. window.setGeometry(5,20,screen.width()-x,screen.height()-y);
    To copy to clipboard, switch view to plain text mode 
    from other functions. I cannot do this since "window" is not reconized by other functions.
    I don't think I can put this implementation
    Qt Code:
    1. MyWidget window;
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: update window size

    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:
    Qt Code:
    1. void MyWidget::resize(int w, int h) //defined as public slot
    2. {
    3. QWidget::resize(w,h);
    4. }
    To copy to clipboard, switch view to plain text mode 

    But there are other ways to go about it, depends on your case.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    eric (13th 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: update window size

    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:
    Qt Code:
    1. connect(Button, SIGNAL(clicked()), this, SLOT(calculate()));
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication application(argc, argv);
    4. MyWidget window;
    5. window.show();
    6. return application.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    If I try to do somethig like
    Qt Code:
    1. window.resize();
    To copy to clipboard, switch view to plain text mode 
    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?
    Last edited by eric; 13th January 2008 at 17:18.

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: update window size

    How do you make the program to emit a signal when program reaches a certain point?
    You should really go through the documentation, 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:
    Qt Code:
    1. emit mySignal();
    To copy to clipboard, switch view to plain text mode 
    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Restore the size of the window after minimize
    By SkripT in forum Qt Programming
    Replies: 10
    Last Post: 22nd September 2016, 16:23
  2. how to change size of window... stupid mistake
    By Sergio_Almonte in forum Qt Programming
    Replies: 2
    Last Post: 30th November 2007, 15:00
  3. window sizes changing even though max size defined
    By JonathanForQT4 in forum Newbie
    Replies: 3
    Last Post: 6th August 2007, 10:39
  4. QTableWidget columns to expand to window size
    By bruccutler in forum Newbie
    Replies: 1
    Last Post: 13th April 2007, 16:02
  5. Replies: 1
    Last Post: 9th February 2007, 09:41

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.