issue223:gtk4
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| issue223:gtk4 [2025/12/01 09:04] – d52fr | issue223:gtk4 [2025/12/01 10:13] (Version actuelle) – d52fr | ||
|---|---|---|---|
| Ligne 6: | Ligne 6: | ||
| Open and view the files person-contact.h, | Open and view the files person-contact.h, | ||
| + | |||
| + | Dans l' | ||
| + | |||
| + | Une capture d' | ||
| + | |||
| + | Le code source complet de ce projet est disponible au téléchargement à l' | ||
| + | |||
| + | Ouvrez et consultez les fichiers person-contact.h, | ||
| Ligne 13: | Ligne 21: | ||
| The code for saving a GListStore of contacts to a .csv file is shown on the next page. GFile is an identifier for a file and is constructed using g_file_new_for_path() which takes one parameter, a string containing the path for the file to be opened. In this case a file called “contacts.csv” is saved to the current working directory. A data output stream is created for writing data directly to a file output stream. | The code for saving a GListStore of contacts to a .csv file is shown on the next page. GFile is an identifier for a file and is constructed using g_file_new_for_path() which takes one parameter, a string containing the path for the file to be opened. In this case a file called “contacts.csv” is saved to the current working directory. A data output stream is created for writing data directly to a file output stream. | ||
| + | |||
| + | Enregistrement des contacts | ||
| + | |||
| + | Il est nécessaire de sauvegarder les contacts stockés dans GListStore afin que les données soient enregistrées sur le disque à la fermeture de l' | ||
| + | |||
| + | Le code permettant d' | ||
| + | |||
| **Creating New Contacts | **Creating New Contacts | ||
| Ligne 28: | Ligne 43: | ||
| Inspecting the code in the download shows that the positioning of widgets is handled with a box container. Widgets are appended to a box container which is a child of the dialog window. Pressing the “Add Contact” button invokes the callback called “callbk_add_new_contact”.** | Inspecting the code in the download shows that the positioning of widgets is handled with a box container. Widgets are appended to a box container which is a child of the dialog window. Pressing the “Add Contact” button invokes the callback called “callbk_add_new_contact”.** | ||
| + | |||
| + | Création de nouveaux contacts | ||
| + | |||
| + | Un nouveau bouton nommé « button_new_contact » est créé dans la fonction activate() puis ajouté à l' | ||
| + | |||
| + | GtkWidget *button_new_contact; | ||
| + | |||
| + | button_new_contact = gtk_button_new_with_label(" | ||
| + | |||
| + | g_signal_connect(button_new_contact, | ||
| + | |||
| + | Dans callbk_new_contact, | ||
| + | |||
| + | L' | ||
| Ligne 41: | Ligne 70: | ||
| A new contact is created using g_object_new(). Then g_object_set() is used to set the values for the person name, email address and phone details from the text retrieved from the entry widgets. The code snip below shows how this is done.** | A new contact is created using g_object_new(). Then g_object_set() is used to set the values for the person name, email address and phone details from the text retrieved from the entry widgets. The code snip below shows how this is done.** | ||
| + | |||
| + | La fonction g_object_set_data() permet d' | ||
| + | |||
| + | g_object_set_data(G_OBJECT(button_add), | ||
| + | |||
| + | Ceci permet d' | ||
| + | |||
| + | GtkWidget *entry_name = g_object_get_data(G_OBJECT(button), | ||
| + | |||
| + | La même approche est utilisée pour associer le bouton « Ajouter » aux champs email et phone. | ||
| + | |||
| + | Un nouveau contact est créé à l'aide de g_object_new(). Ensuite, g_object_set() est utilisé pour définir les valeurs du nom, de l' | ||
| Ligne 50: | Ligne 91: | ||
| g_object_set (contact_new, | g_object_set (contact_new, | ||
| - | |||
| g_list_store_append(store, | g_list_store_append(store, | ||
| Ligne 63: | Ligne 103: | ||
| The g_file_read() | The g_file_read() | ||
| + | |||
| + | PersonContact *contact_new= g_object_new(PERSON_TYPE_CONTACT, | ||
| + | |||
| + | g_object_set (contact_new, | ||
| + | |||
| + | g_object_set (contact_new, | ||
| + | |||
| + | g_object_set (contact_new, | ||
| + | |||
| + | g_list_store_append(store, | ||
| + | |||
| + | save_contacts(store); | ||
| + | |||
| + | Le nouveau contact est ajouté à la GListStore et la méthode « save_contacts() » est appelée pour mettre à jour les contacts enregistrés. | ||
| + | |||
| + | Ouvrir des contacts | ||
| + | |||
| + | À la fermeture de l' | ||
| + | |||
| + | La fonction g_file_read() est utilisée avec le pointeur GFile pour ouvrir le fichier « contacts.csv » en lecture. Ce fichier est supposé se trouver dans le répertoire de travail. GFileInputStream est utilisé pour lire le contenu du fichier. Une vérification est effectuée pour s' | ||
| + | |||
| **A GListStore pointer called store is declared and g_list_store_new() is used to create a new GListStore with items of type G_TYPE_OBJECT which is the fundamental type for GObject. Then g_data_input_stream_new() | **A GListStore pointer called store is declared and g_list_store_new() is used to create a new GListStore with items of type G_TYPE_OBJECT which is the fundamental type for GObject. Then g_data_input_stream_new() | ||
| A while loop is used to read the data lines one by one from the “contacts.csv” file. The C string token function called strtok() is used to break each line string into pieces representing the name, email and phone fields. Each piece of data is separated by a comma and so a comma is used as the delimiter i.e. break character. With strtok() the first parameter is the string and the second is the delimiter and it returns a char pointer to the occurrence of each piece of data. There are three pieces of data which are the field’s name, email and phone and so strtok() is called three times for each line. Notice in subsequent strtok() calls the first parameter is a null pointer to ensure that the position after the end of the last token is used as the new starting location for scanning. See the strtok() documentation in the external links for more information.** | A while loop is used to read the data lines one by one from the “contacts.csv” file. The C string token function called strtok() is used to break each line string into pieces representing the name, email and phone fields. Each piece of data is separated by a comma and so a comma is used as the delimiter i.e. break character. With strtok() the first parameter is the string and the second is the delimiter and it returns a char pointer to the occurrence of each piece of data. There are three pieces of data which are the field’s name, email and phone and so strtok() is called three times for each line. Notice in subsequent strtok() calls the first parameter is a null pointer to ensure that the position after the end of the last token is used as the new starting location for scanning. See the strtok() documentation in the external links for more information.** | ||
| + | |||
| + | Un pointeur GListStore nommé store est déclaré, puis la fonction g_list_store_new() est utilisée pour créer un nouvel objet GListStore contenant des éléments de type G_TYPE_OBJECT, | ||
| + | |||
| + | Une boucle while est utilisée pour lire les lignes de données une par une depuis le fichier « contacts.csv ». La fonction strtok() du langage C, qui manipule les jetons de chaînes de caractères, | ||
| + | |||
| **Selection | **Selection | ||
| Ligne 84: | Ligne 150: | ||
| The same selection approach can be used to expand the application so that a contact can be edited.** | The same selection approach can be used to expand the application so that a contact can be edited.** | ||
| - | **Use the Makefile in the download to build the application which produces an executable called “addressbook”. | + | Sélection |
| - | Hopefully these articles and code examples have provided enough information to get started with GTK4 programming in C. | + | Il est désormais possible d' |
| + | `button_delete_contact = gtk_button_new_with_label(" | ||
| + | `g_signal_connect(button_delete_contact, | ||
| + | |||
| + | Le pointeur de sélection permet d' | ||
| + | |||
| + | `guint p = gtk_single_selection_get_selected((GtkSingleSelection*) selection); | ||
| + | |||
| + | L' | ||
| + | |||
| + | Cette même méthode de sélection peut être utilisée pour étendre l' | ||
| + | |||
| + | |||
| + | **Use the Makefile in the download to build the application which produces an executable called “addressbook”. | ||
| + | |||
| + | Hopefully these articles and code examples have provided enough information to get started with GTK4 programming in C. | ||
| External Links | External Links | ||
| Ligne 94: | Ligne 175: | ||
| GListStore | GListStore | ||
| https:// | https:// | ||
| - | |||
| strtok to split a string into tokens | strtok to split a string into tokens | ||
| https:// | https:// | ||
| + | |||
| + | Utilisez le Makefile inclus dans le téléchargement pour compiler l' | ||
| + | |||
| + | Nous espérons que ces articles et exemples de code vous auront fourni suffisamment d' | ||
| + | |||
| + | Liens externes : | ||
| + | |||
| + | GListStore | ||
| + | https:// | ||
| + | |||
| + | strtok pour découper une chaîne en jetons | ||
| + | https:// | ||
| + | |||
issue223/gtk4.1764576252.txt.gz · Dernière modification : 2025/12/01 09:04 de d52fr
