How to build fixed size windows in gtk 4

I am new to gtk and using gtk4 to build UI for our windows desktop application. We want to build fixed sized windows for all of our UI and do not want to default to gtk for window sizing as different windows could end up different sizes depending on content or content’s css.

I want to specify a fixed window size say 600 * 500 which would not extend no matter the content or its styling (I plan to handle the content part myself). Is this possible with gtk4 ? Any pointers would be highly appreciated. Thank you !

Hi,

You can request a widget to have a minimum size with Gtk.Widget.get_size_request.

(if you call this on the window itself, the size will include the decorations (i.e. the titlebar), so probably you want to apply it on the window’s child instead)

Then, to prevent the window from extending, use Gtk.Window.set_resizable (FALSE).

Gtk.widget.set_size_request enables us to specify just the minimum size like you pointed, but depending on the content, window size would be expanded. And Gtk.Window.set_resizable doesn’t help here because it doesn’t seem to work on initial size of the window rather just whether or not a user can resize the window after its created !!

Yes, it’s how GTK works. If widgets require more space, then the window will grow accordingly.

Either try to control how much your widgets grow, or put them into a ScrolledWindow (you can hide the scrollbars by setting their policy to “external”).

In some cases I would like to fix windows’ height. Width can be changed. Is that possible with gtk4?

The major version of GTK has nothing to do with it: if you want to “fix the size” of a window you have to fix the size of its contents. One way to do it is to set the minimum size of the child of a GtkWindow, but you have to remember this:

  • localization
  • font sizes
  • theme changes
  • accessibility options

can all influence the size of a widget in GTK, which means there’s no such thing as “a fixed size”. GTK will not clamp the size of a window and cut off its contents, because that would be awful. It is your responsibility as an application developer to control the size of the contents of a window, and handle different sizes gracefully.

In other words: GTK does not behave like Visual Basic 6 and WinForms.

In my case, I know the size of the content after the initial window appears, and I can determine if it makes sense to resize in any direction. I have a solution for situations where both width and height are fixed, as the entire window can be set to a fixed size. However, if I have a grid of labels and entries, it doesn’t make sense to resize the window vertically. Do you have any suggestions on how to fix the size on just one side?

That’s not how GTK layout works, sorry.

The content of a window decides how big the window is; if the window is resizable, then it can be resized between the minimum size of its contents, and the size of the display, in either direction; the content can then decide whether it should expand to fit into new available space or not.

I have my own layout managers and just need to know how to specify the minimum and maximum content size of a window. I tried using gtk_widget_measure on a child widget, but it didn’t work for some reason. I also attempted to set the minimum and natural sizes to equal values for the vertical part. Is that the correct approach?

Everything else is working smoothly. On macOS, I’m using Cocoa and can easily fix this. I just need to know how to achieve the same result on Linux/Windows with GTK4.

GTK does not have the concept of a maximum size, only minimum an “natural”, which are not the same. That’s why I said what you want to achieve is not possible.

The only container that has a concept of a content size is GtkScrolledWindow, which controls the threshold for scrolling.

I’m not entirely sure why you’re assuming that just because it happens on Cocoa, then GTK has a way to achieve the same. They are different toolkits, with different policies and mechanisms.

I’m fully aware that these two are different frameworks/toolkits. As someone who prioritizes providing the best user experience when working with UI, I ultimately seek the best solutions. Cocoa offers better control in this regard. It’s unfortunate that limiting one side of a window isn’t currently possible in GTK. Right now, when a user resizes the window vertically (without the controls being resizable vertically), I end up with empty space.

I want to clarify that I’m not criticizing GTK4 - I’m very happy with it. It just seems like this feature could be a valuable addition in future releases.

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.