What is the preferred way to write GTK applications?

I recently started learning how to create GUI applications using GTK + (3), following the instructions on the gnome website (first link below).

So, I began to study the first few textbooks, and everything was wonderful. Here are some of the early codes I wrote:

#include <gtk/gtk.h> static void activate (GtkApplication* app, gpointer user_data){ GtkWidget *window; GtkWidget *button_box; GtkWidget *button; window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), "First application"); gtk_window_set_default_size (GTK_WINDOW (window), 640, 480); button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL); gtk_container_add(GTK_CONTAINER(window), button_box); button = gtk_button_new_with_label("Click this!"); g_signal_connect_swapped(button, "clicked", G_CALLBACK(g_print), "Hello, world!\n"); g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window); gtk_container_add(GTK_CONTAINER(button_box), button); gtk_widget_show_all (window); } int main(int argc, char *argv[]){ GtkApplication *app; int status; app = gtk_application_new ("com.example.application", G_APPLICATION_FLAGS_NONE); g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return status; } 

Everything worked exactly the way I wanted. Then they introduced Gtk Builder, and I thought it was really cool, so I used this to create the application.

However, all of a sudden, they started writing code that did not look anything like everything they wrote earlier (second link below). So, I was very embarrassed and decided to look at the code from other sources to see how they wrote their GTK applications. I saw how they used things like gtk_init() , gtk_main() and gtk_window_new(GTK_WINDOW_TOPLEVEL) . This is not at all like the code I posted earlier.

So, as I understand it now, there are several ways to write GTK applications using either GtkApplication or gtk_init() . One of them is outdated and no longer prefers? If so, which one is preferred or should be preferred?

A few links to the two ways I found in the official Gtk tutorial:

+5
source share
1 answer

Using GtkApplication now the preferred way for new applications. Older gtk_init and gtk_main code calls manually.

gtk_init only initializes GTK + and processes some GTK + command line arguments. You should do almost everything else yourself, even adding code to exit GTK + when the user tries to close the application.

GtkApplication , GtkApplication other hand, calls gtk_init for you and provides a way to manage the materials needed by many applications, such as instance uniqueness, prohibition handling, menu bar controls, etc. Of course, you can write it yourself, but it will take some time for you to write and debug it, and when using GtkApplication you just get these functions for free.

People are also often confused about how to link their code to an event loop system, such as the main GLib loop. Using GtkApplication makes you immediately use the event loop and understand what it is doing to do something. This way all the code you write is triggered in response to the event. Compare this to calling gtk_init manually and with the code in the main, and the rest in event handlers.

+9
source

Source: https://habr.com/ru/post/1263439/


All Articles