Set transient for a template (original) (raw)
Sorry for asking a dumb question, but I’m just starting out…
I started with the code here to build an application with gtk-rs. I added a button to open another gtk window which I would like to set modal and transient for the base window. Setting it modal by putting <property name="modal">true</property>
works fine, however for <property name="transient-for">id</property>
I would need to set an id for the main window. Since the main window is defined as a template and not as an object, I cannot set an id (I think). What should I do?
I guess my problem is that I don’t really understand templates yet…
monster (Jamie Gravendeel) May 7, 2025, 3:05pm 2
Does passing the object in work? In your example, it would be:
<property name="transient-for">MyGtkAppWindow</property>
jdw May 8, 2025, 2:36pm 3
Dear Jamie, thanks for your reply! Unfortunately that does not work. When compiling, I get the following warning:
Gtk-CRITICAL **: 16:33:42.142: Error building template class 'NewMeasurementWindow' for an instance of type 'NewMeasurementWindow': .:0:0 Object with ID ThermometerWindow not found
(I set the template class to ThermometerWindow
rather than MyGtkAppWindow
).
CodedOre (CodedOre) May 8, 2025, 3:18pm 4
Well, here is the thing:
You can, in theory, have multiple “root” windows, to which the window of your template could be set transient to. For example, your ThermometerWindow
could also have multiple instances.
For this reason, the transient-for
property requires a reference to an specific, instanced window. But this is not information you can realistically store in the template file.
For transient-for
, you probably won’t get around setting it in your code instead.
Hi,
How do you define ThermometerWindow
?
Should be something like this to work:
<object class="GtkWindow" id="ThermometerWindow">
<!-- ... -->
</object>
That way you get an object instance as expected.
monster (Jamie Gravendeel) May 9, 2025, 9:46am 6
I’m imagining it’s a template, so something like
<template class="ThermometerWindow" parent="GtkApplicationWindow">
<!-- ... -->
</template>
I understood the template is for the main window NewMeasurementWindow
, while ThermometerWindow
is a sub-window.
There can’t be more than one template
per ui file, so one of these windows shall be defined as a simple object.
jdw May 14, 2025, 2:29pm 8
Yes, @monster is right, I defined both my ThermometerWindow
and the NewMeasurementWindow
as templates. Thanks to @CodedOre’s hint “setting it in your code” instead, I solved my issue by defining the function
impl NewMeasurementWindow {
pub fn new(window: &Window) -> Self {
// Create new window
Object::builder().property("transient-for", window).build()
}
}
and then creating the subwindow as
let new_measurement_window = NewMeasurementWindow::new(window);
where window is my main ThermometerWindow.
system (system) Closed June 13, 2025, 2:30pm 9
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.