Search code examples
c++gtkmmgtkmm3

How can I use gtk_window_set_transient_for ()?


I am using the library gtkmm. My code is almost perfect, I think because it compiles and I can execute it. But in the terminal when I clicked on open a file in my software that I made with gtkmm I can read this message :

Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.

So I looked for on this forum how can I solve it and I understood I have to use this method : gtk_window_set_transient_for().

Actually I have a Gtk::Window and a Gtk::FileChooserDialog. Can you put an example which use gtk_window_set_transient_for() ?

Thank you very much !


Solution

  • Gtk::FileChooserDialog and other GTK+ dialogues are derived from Gtk::Window. Gtk::Window has the method set_transient_for(Gtk::Window &parent); which if not set gives you the message you have seen.

    To fix this set_transient_for(Gtk::Window &) needs to be used. Note that this takes a reference and not a pointer. So you would use it something like this.

    {
      Gtk::Window first_window;
    
      ...
    
      Gtk::FileChooserDialog file_dialog("Title");
    
      ...
    
      file_dialog.set_transient_for(first_window);
    
      ...
    }
    

    It is also possible to set the transient window for the dialog with the constructor. like so.

    {
      Gtk::Window first_window;
    
      ...
    
      Gtk::FileChooserDialog file_dialog(first_window, "Title");
    
      ...
    }
    

    If first_window is a pointer the you would need to do something like so.

    file_dialog.set_transient_for(*first_window);