issue223:gtk4
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| issue223:gtk4 [2025/12/01 08:15] – créée d52fr | issue223:gtk4 [2025/12/01 10:13] (Version actuelle) – d52fr | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | In the previous article a contact class which inherited from GObject was created and used to add some fictitious contacts to a GListStore so that they could be viewed using GtkColumnView. This article shows how to expand the application into a simple address book. This requires that a new contact can be created and a selected contact deleted. | + | **In the previous article a contact class which inherited from GObject was created and used to add some fictitious contacts to a GListStore so that they could be viewed using GtkColumnView. This article shows how to expand the application into a simple address book. This requires that a new contact can be created and a selected contact deleted. |
| A screenshot of the application is shown below which has been developed using Ubuntu 24.04. Again fictitious individuals, | A screenshot of the application is shown below which has been developed using Ubuntu 24.04. Again fictitious individuals, | ||
| Ligne 5: | Ligne 5: | ||
| The full source code for this project and can be downloaded using the web link: https:// | The full source code for this project and can be downloaded using the web link: https:// | ||
| - | Open and view the files person-contact.h, | + | Open and view the files person-contact.h, |
| + | Dans l' | ||
| - | Saving Contacts | + | 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, | ||
| + | |||
| + | |||
| + | **Saving Contacts | ||
| A way is needed to save contacts stored in the GListStore so that when the application is closed data has been saved to disk. In this example contact details will be saved to a .csv file. The extension “csv” stands for comma-separated value. With a .csv file each line in the file represents a contact record and the values (e.g. the name, email and phone fields) within each record are separated by commas. This format is easily viewed using a text editor or spreadsheet. | A way is needed to save contacts stored in the GListStore so that when the application is closed data has been saved to disk. In this example contact details will be saved to a .csv file. The extension “csv” stands for comma-separated value. With a .csv file each line in the file represents a contact record and the values (e.g. the name, email and phone fields) within each record are separated by commas. This format is easily viewed using a text editor or spreadsheet. | ||
| - | 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. |
| - | Creating New Contacts | + | Enregistrement des contacts |
| - | A new button called button_new_contact is created within the activate() function and then added to the window header. The g_signal_connect() function connects the button clicked signal to the callback called “callbk_new_contact” using the store as the gpointer parameter. | + | 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 | ||
| + | |||
| + | A new button called button_new_contact is created within the activate() function and then added to the window header. The g_signal_connect() function connects the button clicked signal to the callback called “callbk_new_contact” using the store as the gpointer parameter. | ||
| Ligne 29: | Ligne 42: | ||
| Within “callbk_new_contact” a dialog window is created and entry widgets are used for capturing the contact name, email and phone. | Within “callbk_new_contact” a dialog window is created and entry widgets are used for capturing the contact name, email and phone. | ||
| - | 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 | ||
| - | The function g_object_set_data() allows an association to be made between the “button_add” and the entry called “entry_name” using the key parameter named “entry-name-key” as shown below. | + | 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' | ||
| + | |||
| + | |||
| + | **The function g_object_set_data() allows an association to be made between the “button_add” and the entry called “entry_name” using the key parameter named “entry-name-key” as shown below. | ||
| g_object_set_data(G_OBJECT(button_add), | g_object_set_data(G_OBJECT(button_add), | ||
| Ligne 42: | Ligne 69: | ||
| The same approach is used to make associations between the “button_add” and the email and phone entries. | The same approach is used to make associations between the “button_add” and the email and phone entries. | ||
| - | 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' | ||
| - | PersonContact *contact_new= g_object_new(PERSON_TYPE_CONTACT, | + | 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' | ||
| + | |||
| + | |||
| + | **PersonContact *contact_new= g_object_new(PERSON_TYPE_CONTACT, | ||
| g_object_set (contact_new, | g_object_set (contact_new, | ||
| Ligne 52: | Ligne 91: | ||
| g_object_set (contact_new, | g_object_set (contact_new, | ||
| - | |||
| g_list_store_append(store, | g_list_store_append(store, | ||
| Ligne 64: | Ligne 102: | ||
| When the application is closed the contacts remain saved in the “contacts.csv” file located in the working directory. | When the application is closed the contacts remain saved in the “contacts.csv” file located in the working directory. | ||
| - | The g_file_read() | + | The g_file_read() |
| - | 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() | + | PersonContact *contact_new= g_object_new(PERSON_TYPE_CONTACT, |
| - | 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 | + | g_object_set |
| - | Selection | + | g_object_set (contact_new, |
| - | Everything can now be glued together to create a simple address book application to store contacts. A GtkSingleSelection *selection pointer can be created and used to select a single contact in the column_view so that it can be deleted. This is done by passing the selection pointer to a callback. An example is shown below for a button to delete a selected contact when clicked. | + | 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 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 | ||
| + | |||
| + | Everything can now be glued together to create a simple address book application to store contacts. A GtkSingleSelection *selection pointer can be created and used to select a single contact in the column_view so that it can be deleted. This is done by passing the selection pointer to a callback. An example is shown below for a button to delete a selected contact when clicked. | ||
| button_delete_contact = gtk_button_new_with_label(" | button_delete_contact = gtk_button_new_with_label(" | ||
| Ligne 86: | Ligne 148: | ||
| The index position can then be used to remove a contact from the GListStore using g_list_store_remove(). The save_contacts() method is called after a contact has been removed so that it is no longer saved in the “contacts.csv “ data file. | The index position can then be used to remove a contact from the GListStore using g_list_store_remove(). The save_contacts() method is called after a contact has been removed so that it is no longer saved in the “contacts.csv “ data file. | ||
| - | 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 98: | Ligne 175: | ||
| GListStore | GListStore | ||
| https:// | https:// | ||
| - | |||
| strtok to split a string into tokens | strtok to split a string into tokens | ||
| + | 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:// | https:// | ||
| + | |||
issue223/gtk4.1764573310.txt.gz · Dernière modification : 2025/12/01 08:15 de d52fr
