I currently set a QDialog to have a fixed size using the following code
dlg->setWindowModality(Qt::WindowModal);
dlg->setFixedSize(dlg->size());
Now as a result of this code whatever size I save my ui form in QT Designer. It sticks to that size.This however ends up being a issue in some systems and displays. Where my Qlabels begin to cut from the sides due to the shortage of space.I wanted to know what would be the proper way of doing this ? How would I determine which size would accommodate the layout on the form.The form itself has a horizontal layout which has multiple layouts in it ??
Try
dlg->adjustSize();
dlg->setFixedSize(dlg->sizeHint());
Related
I've got a Qt app with a window that contains a QScrollArea (plus a few small fixed-size controls around the borders, but window is mainly taken up by the QScrollArea).
Inside the QScrollArea is a fixed-size view of various widgets that the user can scroll across (using the scroll bars) when the window has been sized too small to fit the entire view.
There are some "Show Foo" and "Hide Foo" type menu items that allow the user to customize (to some extent) what widgets are visible inside the fixed-sized view, and when the user selects one of these menu items, the size of the fixed-sized view changes as the corresponding widgets are hidden or shown.
This all works okay; however I'd like to have the maximum-size of the window always be set to just large enough to allow the user to see the entire fixed-sized view without scrolling; there's no point in allowing the user to resize the window any larger than that, since the extra space would just be empty and useless.
I currently implement this behavior by manually calculating the maximum-window-size based on the current size of the fixed-size view and calling setMaximumSize() on the window. However, this technique seems very fragile, since it depends not only on calculating the maximum size correctly (which is doable) but also calculating it at the correct time (i.e. calculate it too soon and the resulting size might not include all the new widgets yet, resulting in the maximum-window-size getting set too small).
That makes me wonder, is there some better way to achieve this result? This seems like it might be a fairly common use-case in Qt GUIs that use QScrollArea.
I'm still trying to do some pretty basic stuff with qt, and it's been a struggle. I'm specifically targeting for Mac. My current problem is getting the forms to not suck.
The simple problem. Create a new MainWindow app. Go into the Qt Creator (open Forms -> mainwindow.ui).
Drag 3 labels into place. Then I dragged 2 Line Edits and for fun, a Dial, but that part probably doesn't matter.
Click the main window and then tell it to use Form Layout. Within Qt Creator if I resize the window, my various line edits expand to fill the available space, exactly like I want.
Run the app. All the line edit fields are a fixed length, very short (if I don't override their minimum width), and do NOT resize as I resize the window.
Three quarters of the reason to use Form Layout is for resizing capability.
I've tried clicking on the central widget before setting to Form Layout: no change in behavior.
I do get reasonable behavior if I use a grid layout, although I have to add a vertical spacer at the bottom or my dial resizes crazily as I play with the window size.
So... Am I just doing something wrong with Form Layout? Or does it not work well on Mac and I should use Grid Layout instead?
I really miss Motif's XmFormLayout. It took time to set all the constraints, but I could make my forms do exactly what I wanted. Ah, but so 1990s.
On the panel to the right where you can see the objects, click the centralWidget. In the property panel, scroll down until you get to the layout properties of the form layout. Set the layoutFieldGrowthPolicy to ExpandingFieldsGrow.
By default, the line edits are not growing to fill the available space on macOS because they are not supposed to. This is some sort of macOS UI guideline. QFormLayout follows the platform conventions by default.
It would also be nice if I can figure out how to NOT necessarily expand everything to full width. I have a few Line Edit widgets that I'd like to keep short. They're going to hold TCP port numbers.
For those, set the Horizontal Policy property (it's grouped inside the sizePolicy property group in the properties panel) of the affected line edits to something suitable. In this case, probably to Fixed, but look up the documentation first of what each setting does.
I want to resize everything when i show it in full screen automatically keeping aspect ratio. How can i do it. i use PyQt4 but if you know of C++ then also please tell me.
Hope the code will be not more than 2-3 lines.
Thanks
I suppose you've implemented static layouts with fixed widget sizes and positions in your dialogs/windows. In order to resize the widgets with the window, what you need to do is using dynamic layouts. You best use Qt Designer for the dialog design, else setting up any non-trivial layout can get rather toilsome.
I'm developing a Qt application and it's currently in an internal beta test. One member of the company has Windows configured to display text larger than its normal size, which breaks my UI. The About page, for example, currently looks like this:
but under his settings, looks like this (note the clipped text):
Coming from a C#/Winforms background, I'm amazed that I can't seem to find some easily configurable label property such as Form.AutoSize that will automatically size the labels to fit their containing text. I've tried messing with sizePolicy, scaledContents, and a few other properties, but none seem to do this.
I've come across various threads (such as this one) which give instructions for scaling the text to the label, but I want to do the opposite - scale the label to the text to facilitate for those with enlarged text settings like my co-worker. Is there a straightforward way to do this?
There are at least three solutions to this problem.
Use layouts. Their contents are scaled according to the size of the window.
Make a code which is executed whenever window size is changed. In that code, you get the width of the longest text in the window (How?)(another way) and then set window wider than that.
Do the same as in solution #2, but execute the code only when the dialog is shown. After that, alter the window properties so that its size cannot be changed.
I'm working on a Qt project and I've noticed a persistent problem with some GUI forms. The form looks fine on KDE (bottom picture) and Windows , but when the app runs on anything GNOME3-based (like Unity or GNOME3 itself) some parts of the form are hidden from view. (Top picture, everything below the Sort Ascending radio button is cut off)
The problem seems to be with how Qt layouts handle large font sizes. If the user is using normal-sized system font (<= 10pt) everything works fine. If they are using larger fonts, the form is not large enough to accommodate everything. Other forms affected by this bug are merely crowded, but that isn't as serious as having vital controls out of bounds. The layout doesn't want to resize itself to take advantage of new space if I enlarge the dialog. Is there an easy way to make it do this or do I need to hard-code it? Originally the code prevented dialog resizing during runtime, but restoring that functionality didn't fix the bug. Even if the dialog can expand, the problem is the layout won't expand with it.
Up until now, I've made all affected forms oversized to compensate for this bug, but it looks strange to have the dialogs much bigger than they need to be on Windows and KDE systems where the font is the proper size. Is there a way to cause an affected dialog/layout to resize itself so everything fits properly at runtime? If so, how would the program detect it when parts of the GUI are out of bounds? I would prefer not to force a certain font size (some people may prefer large fonts due to vision problems).
Thanks in advance for help.
The fix for this is using a different approach when displaying forms. A more dynamic way as I'll describe. I've successfully used this approach on Windows with 96 and very high DPI modes (over 120).
1.
Query the OS and get the user's chosen font for a particular system item; say the font used for the window caption or system dialog boxes. Also you could allow the user to choose their font later if they desired. Use True Type fonts when doing this if possible.
2.
Using that font, construct a string object that you'll use for a label or edit control (I don't know what this is for QT, for Windows it is GetTextExtentPoint32) and pass it to a system function to determine the width and height of the string for your environment.
3.
Given the above value, place the control and dynamically resize the form with the padding all around the control as you like. For buttons you might always add a certain percentage of pixels above and below the button to taste.
4.
For graphical elements like Bitmaps and jpegs, again query the OS for the current DPI settings of the monitor and use larger, pre-made resources. Naturally, all text around theses elements will be dynamically placed on the fly.
Note that on Windows you'll need to mark your exe as high dpi aware using a manifest.