I'm using GTK library to make a simple GUI for my application but I'm having some problem managing window navigation. In particular, I'm not able to come back from a window to a previous one displayed on the screen. I've already tried multiple possibilities such as: gtk_window_set_focus
, gtk_window_present
but they don't work. Here's the code:
void callback(GtkWidget *wid, gpointer ptr)
{
GtkWidget *win = static_cast<GtkWidget*>(ptr);
gtk_window_fullscreen(GTK_WINDOW(win));
}
int main()
{
gtk_init(NULL,NULL);
GtkWidget *window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window1), "1th window");
gtk_window_fullscreen(GTK_WINDOW(window1));
GtkWidget *btn1 = gtk_button_new_with_label ("Go to 2th window");
gtk_container_add (GTK_CONTAINER (window1), btn1);
gtk_widget_show_all(window1);
GtkWidget *window2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window2), "2th window");
GtkWidget *btn2 = gtk_button_new_with_label ("Go back to 1th window");
gtk_container_add (GTK_CONTAINER (window2), btn2);
g_signal_connect(window1,"delete-event",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(window2,"delete-event",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect (btn1, "clicked", G_CALLBACK (callback),window2);
g_signal_connect (btn2, "clicked", G_CALLBACK (callback),window1);
gtk_widget_show_all(window2);
gtk_main();
}
The problem is that when I push btn2, even if callback function is invoked, the previous window does not become the active one.
Change your handler for this (fullscreen line removed for simplicity):
void callback(GtkWidget *wid, gpointer ptr)
{
// This, by itself, does nothing (see next line):
GtkWindow *win = static_cast<GtkWindow*>(ptr);
// This changes the active window on screen:
gtk_window_present(win);
}
The trick is to use gtk_window_present
inside the handler (your handler does nothing except put in fullscreen).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.