1

I have developed my Application with most of the Widgets in Qt Creator's Designer module. I have hard coded the sizes of my widgets depending on how they appeared on my laptop. Yesterday when I ran the application on my Desktop (lower screen resolution than laptop) some of the Main Window widgets were not visible & on startup there was horizontal scrollbar & I had to scroll to see those widgets; which is not the case on my laptop. I have 2 problems:

  1. Is it possible to resize all of my widgets (even those which are added run time by code) by using some resize factor? Maybe at startup I will get the screen resolution of Hardware in which application is running & then create a ratio of that with resolution of my laptop. Is it possible to multiply this factor to all widgets without adding code for each widget?

  2. How do I get the resolution of the Screen in which my Application is running?

PS: I would like to know the defacto method of solving this problem... Thank You.

Cool_Coder
  • 4,888
  • 16
  • 57
  • 99
  • Why would you hardcode some positions, when Qt can resize everything automatically. Hardcoded sizes of widgets is a bad habbit, fix it before too late. Window resizing would work this way also, as it is not based on DPI or Hardware, but windows size and font DPI settings. Just use layouts as suggested. – Pihhan Mar 12 '13 at 13:29

2 Answers2

2

You should use Layouts to manage the size policy of your widgets.

Qt layouts automatically position and resize widgets when the amount of space available for them changes, ensuring that they are consistently arranged and that the user interface as a whole remains usable."

You could also check this question for more information regarding layout machanisms in Qt.

Qt website has got excelent documentation on the subject. You can start here for more information on working with layouts in Qt Designer.

Community
  • 1
  • 1
bruno
  • 2,802
  • 1
  • 23
  • 23
  • That question looks interesting! But you are not getting my point. I want to resize the main window according to the screen resolution of user's hardware! & I want to be able to resize other widgets lik PushButton, LineEdit, Label, etc – Cool_Coder Mar 12 '13 at 13:27
  • But isn't that what the layout classes do? When the resolution of the screen changes, the size of your widgets will be automatically set so that they are consistently arranged. – bruno Mar 12 '13 at 13:35
  • so what size policiy should I use? Currently I am using "fixed" along with setting the "minimum" values according to my wish. If this is not the correct method then what should I do so that the application appears same everywhere it is run irrespective of the hardware? – Cool_Coder Mar 12 '13 at 13:38
  • currently I am using QFrame for most of the widgets & giving a layout to the frame. I am not explicitly adding a Layout item as available in the list on left side in designer. – Cool_Coder Mar 12 '13 at 13:39
  • I don't have Qt Designer at hand, but if I remember correctly the size of the widgets should be set to expanding. You add a new layout management class: QHBoxLayout, QVBoxLayout, QGridLayout, and move the widgets into these layouts. Other way is to select some of the widgets, right-click and select the layout option that suits your needs. Sorry for not being able to provide more help, but as i've told I don't have access to Qt Designer right now. – bruno Mar 12 '13 at 13:54
2

You could try a function like this:

resize(theDesktop->screenGeometry().width()*PERCENTAGE_OF_MONITOR_WIDTH_FOR_SCREEN, theDesktop->screenGeometry().height()*PERCENTAGE_OF_MONITOR_HEIGHT_FOR_SCREEN);

Resize is a member function of the QWidget class and the PERCENTAGE_OF_MONITOR variables would be whatever percentage of the monitor you want your application to take up.

theDesktop is of the type QDesktopWidget.

rmarshw
  • 46
  • 4