最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - Why doesn't the child window pop up on top of the parent window? - Stack Overflow

programmeradmin1浏览0评论

I am trying to create a preferences window for my app. I want the child-window (preferences) to open above the parent-window (main-window) and in the middle of the parent-window. But the problem is, when running the app for the first time, the child window is not launching on top of the parent window. But when i close the child window(preferences) and open it again, it opens on top of the parent window.

Note: parent-window = main-window. & child-window = preferences.

#include <gtk/gtk.h>

void preferences_cb(GtkButton *button, gpointer data)
{
    GtkWidget *widget;
    widget = (GtkWidget *) data;
    if (widget == NULL)
    return;
      gtk_widget_realize(widget);
      gtk_window_present ((GtkWindow *)widget);
    return;
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *grid;
  GtkWidget *button; 
  GtkWidget *preferences;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "parent window");
  gtk_widget_set_size_request(window, 700, 200);
  gtk_window_set_resizable (GTK_WINDOW(window), TRUE);

  grid = gtk_grid_new ();
  gtk_window_set_child ((GtkWindow *)window, grid);
  gtk_widget_set_vexpand(grid, TRUE);
  gtk_widget_set_hexpand(grid, TRUE);

  button = gtk_button_new_with_label("Show preferences.win..");
  gtk_grid_attach ((GtkGrid *)grid, button, 0, 0, 80, 22); 
  gtk_widget_set_vexpand(button, TRUE);
  gtk_widget_set_hexpand(button, TRUE);

  preferences = gtk_window_new();
  gtk_window_set_title (GTK_WINDOW (window), "preferences");
  gtk_window_set_transient_for ((GtkWindow *)preferences, (GtkWindow *)window);
  gtk_window_set_default_size(GTK_WINDOW(preferences), 400, 500);
  gtk_window_set_hide_on_close (GTK_WINDOW(preferences), TRUE);
  gtk_window_set_resizable (GTK_WINDOW(preferences), FALSE);    

  g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(preferences_cb), preferences);
  gtk_window_present ((GtkWindow *)window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;
  #if GLIB_CHECK_VERSION(2, 74, 0)
      app = gtk_application_new (".gtk.example", G_APPLICATION_DEFAULT_FLAGS); 
  #else
       app = gtk_application_new (".gtk.example", G_APPLICATION_FLAGS_NONE); 
  #endif
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

I am trying to create a preferences window for my app. I want the child-window (preferences) to open above the parent-window (main-window) and in the middle of the parent-window. But the problem is, when running the app for the first time, the child window is not launching on top of the parent window. But when i close the child window(preferences) and open it again, it opens on top of the parent window.

Note: parent-window = main-window. & child-window = preferences.

#include <gtk/gtk.h>

void preferences_cb(GtkButton *button, gpointer data)
{
    GtkWidget *widget;
    widget = (GtkWidget *) data;
    if (widget == NULL)
    return;
      gtk_widget_realize(widget);
      gtk_window_present ((GtkWindow *)widget);
    return;
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *grid;
  GtkWidget *button; 
  GtkWidget *preferences;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "parent window");
  gtk_widget_set_size_request(window, 700, 200);
  gtk_window_set_resizable (GTK_WINDOW(window), TRUE);

  grid = gtk_grid_new ();
  gtk_window_set_child ((GtkWindow *)window, grid);
  gtk_widget_set_vexpand(grid, TRUE);
  gtk_widget_set_hexpand(grid, TRUE);

  button = gtk_button_new_with_label("Show preferences.win..");
  gtk_grid_attach ((GtkGrid *)grid, button, 0, 0, 80, 22); 
  gtk_widget_set_vexpand(button, TRUE);
  gtk_widget_set_hexpand(button, TRUE);

  preferences = gtk_window_new();
  gtk_window_set_title (GTK_WINDOW (window), "preferences");
  gtk_window_set_transient_for ((GtkWindow *)preferences, (GtkWindow *)window);
  gtk_window_set_default_size(GTK_WINDOW(preferences), 400, 500);
  gtk_window_set_hide_on_close (GTK_WINDOW(preferences), TRUE);
  gtk_window_set_resizable (GTK_WINDOW(preferences), FALSE);    

  g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(preferences_cb), preferences);
  gtk_window_present ((GtkWindow *)window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;
  #if GLIB_CHECK_VERSION(2, 74, 0)
      app = gtk_application_new (".gtk.example", G_APPLICATION_DEFAULT_FLAGS); 
  #else
       app = gtk_application_new (".gtk.example", G_APPLICATION_FLAGS_NONE); 
  #endif
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}
Share Improve this question asked Mar 18 at 14:12 dibyendudibyendu 4423 silver badges8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

As far as I know it, the window position is determined by the window system of the operating system independently of GTK.

So you can't do anything.

However, I have changed one thing in the script:

...
preferences = gtk_window_new();
gtk_window_set_title (GTK_WINDOW (preferences), "preferences"); // not (window)
...

And with Ubuntu it works now the way I would expect to do after studying your script.

Enjoy testing and programming.

发布评论

评论列表(0)

  1. 暂无评论