Results 1 to 2 of 2

Thread: How to handle the size of a window

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How to handle the size of a window

    Hi there!

    I have a somewhat complex GUI application on my hands and I don't like how it behaves in terms of sizing of widgets. It is a QMainWindow that has a blank central widget that is managed by a layout. Inside the layout is among small and unimportant things on the side, a large QGraphicsView and the QGraphicsView has a QGraphicsScene, of course. So far, the size of the window that opens up is defined in a very hacky way by setting the scene rect to a certain size in the constructor of the graphics scene. When I remove that line, the window opens up to a seemingly default, way too small size. I am looking for a clean solution that implements the following requirements.

    - When the application is launched, the window should open up to a predetermined fixed size of say 1024x768, or even better two thirds of the actual resolution available on the screen. Then, the user can resize the window as he pleases.
    - The contents of the graphics scene are often changed entirely with the press of a button when the user cycles through geometric scenes of different sizes. Whenever such a switch occurs, the scene should scale according to the current size of the window, i.e. show the entire scene in the available space and it is up to the user to pan and scale as he pleases afterwards.

    Can anyone give me some pointers to the best ways of solving these requirements?

    - Where is the best place to define the initial size of the window?
    - How do I obtain the screen resolution? (I saw different ways in Google, not sure which way is best)
    - Since the contents of the scene sometimes change entirely, I would like to leave the scene rect unset and have Qt handle it automatically. Is that correct?
    - I would implement the panning and zooming functions using the scale() and translate() functions of the graphics view. Right? The scrollbars are always off, this is all done with the mouse.
    - When a scene change occurs, I would use the fitInView() function with the scene rect to make the entire scene visible, right?
    - The graphics scene should know nothing about the size of the window and widgets it is embedded in and should certainly not influence the size of the window in any way. Right?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to handle the size of a window

    When the application is launched, the window should open up to a predetermined fixed size of say 1024x768, or even better two thirds of the actual resolution available on the screen. Then, the user can resize the window as he pleases.

    Where is the best place to define the initial size of the window?

    How do I obtain the screen resolution?
    This is probably best done in the MainWindow's showEvent(). In my apps, I use QSettings to store the last-used window geometry (on closeEvent()) and retrieve it on showEvent(), then use move() and resize() in the showEvent() to set the geometry. For first-time startup, I use a reasonable default size like 1000 x 1000 and positioned somewhere in the upper left (like 50, 50). Or you could make some calls and determine the actual available geometry from the QDesktopWidget.

    I have a little helper class (CSAQtSettingsHelper) that does some of the QSettings overhead (like format, user / system, etc.) but I use it in the MainWindow's showEvent() and closeEvent() like this:
    Qt Code:
    1. // showEvent():
    2. resize( CSAQtSettingsHelper::ReadValue( "MainWindow", "Size", QSize( 800, 800 ) ).toSize() );
    3. move( CSAQtSettingsHelper::ReadValue( "MainWindow", "Position", QPoint( 50, 50 ) ).toPoint() );
    4. restoreState( CSAQtSettingsHelper::ReadValue( "MainWindow", "State" ).toByteArray(), sStateVersion );
    5.  
    6. // closeEvent():
    7. CSAQtSettingsHelper::WriteValue( "MainWindow", "Size", size() );
    8. CSAQtSettingsHelper::WriteValue( "MainWindow", "Position", pos() );
    9. CSAQtSettingsHelper::WriteValue( "MainWindow", "State", saveState( sStateVersion ) );
    To copy to clipboard, switch view to plain text mode 

    Save / restore state work on the toolbars, dock widgets, and other decorations on QMainWindow.

    Since the contents of the scene sometimes change entirely, I would like to leave the scene rect unset and have Qt handle it automatically. Is that correct?
    Reimplement the resizeEvent() for your MainWindow class. After ensuring that the widget is visible (resizeEvent can be called several times before the showEvent, but the geometry is usually not correct until after the widget is visible), call QGraphicsView::fitInView(). Pay attention to the comment about scrollbars and avoiding resizeEvent recursion.

    I would implement the panning and zooming functions using the scale() and translate() functions of the graphics view. Right?
    I would do it by calling QGraphicsView::setSceneRect(). This establishes the region of the scene that is mapped to the view's display. Scaling and translation are automatically taken care of.

    When a scene change occurs, I would use the fitInView() function with the scene rect to make the entire scene visible
    Yes, with the understanding that it is the scene's scene rect you use for this instead of the view's scene rect that you used above for panning / zooming. Since a scene can be mapped to multiple views, the scene's rect is defined by the objects contained in the scene, whereas the view's scene rect is the portion of the scene's rect that is visible. The scaling of that rect to the screen is accomplished by fitInView (among other possibilities).

    The graphics scene should know nothing about the size of the window and widgets it is embedded in and should certainly not influence the size of the window in any way. Right?
    Right. Just like the separation between QAbstractItemModel and QAbstractItemView. The model (scene) gives an abstract description of its contents, their positions relative to the scene and each other, their display properties (colors, fonts etc)., but it is the view's responsibility to map the visible portion of the scene onto its display surface.

    I have found this difficult to accomplish in practice if you want text labels on things that do not scale along with the rest of the items in a screen (say, if you want to label a point in a plot without the text becoming huge and pixelated as you zoom in). If you want the text to be the same size no matter which view it is displayed in, you can't change the properties of the text objects in the scene without affecting all views.

    I have worked around this by having two scenes in a subclassed QGraphicsView - one scene that contains the graphics objects and is view-independent, and another one for the text that is assigned to a specific view and has font scaled to the size of the view and invariant to transformation. The two scene rects are kept in sync when zooming or panning, and the second scene is drawn in the view's drawForeground() method. There might be other / better ways to do this, but using the text overlay scene has worked well for me. I haven't found a good way to set up a scene in world coordinates and set fixed screen-based font sizes without letting the scene know about the resolution.
    Last edited by d_stranz; 22nd March 2018 at 18:28.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    Cruz (28th March 2018)

Similar Threads

  1. QSlider: Increase handle size -> How ?
    By ArneBurghardt in forum Qt Programming
    Replies: 6
    Last Post: 11th August 2016, 21:58
  2. Handle re-sizing of a Window
    By zschoenb in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2015, 20:21
  3. Replies: 2
    Last Post: 8th December 2013, 06:53
  4. Replies: 1
    Last Post: 10th April 2012, 17:18
  5. Getting the handle of the window
    By Luc4 in forum Qt Programming
    Replies: 13
    Last Post: 16th May 2010, 13:02

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.