GTK gossip: gtkcombobox and gtkliststore

Source: Internet
Author: User
Tags gtk
In
Gtkcombobox


In the example, it is the most simple usage of gtkcombobox, and only the literal is selected. If you want to make gtkcombobox show more functions and appearances, you must understand the Model-View Design of gtkcombobox.

The external component of the gtkcombobox is the part of the view object. The part of the selected item is the part of the model object.
The objects in the gtktreemodel interface, such as the gtkliststore or gtktreestore, are actually stored and accessed by the data volume of the model object.
Introduce the use of gtkcombobox and gtkliststore.

Gtkliststore does not have any hierarchical data. When a drop-down list is required, you can directly list the selected data. You can set texts, tabs, and components in gtkliststore, to create a gtkliststore, you must specify that you want to create a unique bit and the type in the specific bit. For example:

Gtkliststore * store = gtk_list_store_new (2, gdk_type_pixbuf, g_type_string );


This program fragment will create a two-digit gtkliststore, a memory storage part, and use gdk_type_pixbuf to specify that the memory bit will be stored in gdkpixbuf, use g_type_string to specify another token.

You can use gdk_pixbuf_new_from_file () to retrieve the vertex and restore it back to gdkpixbuf. The second vertex number is gerror. If you do not need it, you can set it to NULL:

Gdkpixbuf * pixbuf = gdk_pixbuf_new_from_file (files [I], null );



Gtkliststore uses gtktreeiter as the index of the content location. When you use gtk_list_store_append (),
Gtktreeiter points to the location of the next column in gtkliststore. Then, you can configure the gtkliststore with gtk_list_store_set ().
The metadata of the location, for example:

Gtktreeiter ITER;


Gtk_list_store_append (store, & ITER );


Gtk_list_store_set (store, & ITER,


0, pixbuf,


1, filename,


-1 );

The first two parts of gtk_list_store_set () are the gtkliststore and gtktreeiter, and the last two parts are the specified index and resource, end with-1.

With the gtlliststore model object, you can create a view, that is, gtkcombobox. You can use gtk_combo_box_new_with_model () to create a view:

Gtkwidget * ComboBox =


Gtk_combo_box_new_with_model (gtk_tree_model (store ));


How do you display your information? The corresponding gtkcellrenderer must be used.
Gtkcellrenderer and the correlation between the specified vertex must be informed, and gtkcelllayout must be implemented.
The gtkcelllayout interface. Therefore, you can use gtk_cell_layout_pack_start () to set the gtkcellrenderer expiration
Which parameter is used, and use gtk_cell_layout_set_attributes () to set the relevance:

Gtkcellrender * render;

Renderer = gtk_cell_renderer_pixbuf_new ();


Gtk_cell_layout_pack_start (gtk_cell_layout (ComboBox), Renderer, false );


Gtk_cell_layout_set_attributes (gtk_cell_layout (ComboBox), Renderer,



"Pixbuf", 0, // "pixbuf" sets the image




Null); // end with null



Renderer = gtk_cell_renderer_text_new ();


Gtk_cell_layout_pack_start (gtk_cell_layout (ComboBox), Renderer, false );


Gtk_cell_layout_set_attributes (gtk_cell_layout (ComboBox), Renderer,



"Text", 1, // "text" set text




Null );


These are the basic processes for setting the model and view of the gtkcombobox. If you want to obtain the optional information, you must first obtain the optional information.
Model, that is, the gtkliststore in the ctkcombobox, and obtain the selected gtktreeiter (remember? Gtktreeiter refers
To a column in gtkliststore), and then use gtk_tree_model_get () to obtain the expected vertex value. For example:

Gboolean combo_changed (gtkcombobox * ComboBox, gtklabel * label ){


Gtktreemodel * model = gtk_combo_box_get_model (ComboBox );


Gtktreeiter ITER;


Gchar * active;


Gtk_combo_box_get_active_iter (ComboBox, & ITER );


Gtk_tree_model_get (model, & ITER,



1, & active,




-1 );




Gtk_label_set_text (Label, active );


}


In combination with the above description, let's change it.

Gtkcombobox


In the example, let the drop-down list have a small example:

  • Gtk_combo_box_with_icon_demo.c
#include <gtk/gtk.h>

enum {
PIXBUF_COL,
TEXT_COL
};

GtkTreeModel* createModel() {
const gchar *files[] = {"caterpillar.jpg", "momor.jpg",
"hamimi.jpg", "bush.jpg"};
GdkPixbuf *pixbuf;
GtkTreeIter iter;
GtkListStore *store;
gint i;

store = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);

for(i = 0; i < 4; i++) {
pixbuf = gdk_pixbuf_new_from_file(files[i], NULL);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter,
PIXBUF_COL, pixbuf,
TEXT_COL, files[i],
-1);
gdk_pixbuf_unref(pixbuf);
 }

return GTK_TREE_MODEL(store);
}

gboolean combo_changed(GtkComboBox *comboBox, GtkLabel *label) {
GtkTreeModel *model = gtk_combo_box_get_model(comboBox);
GtkTreeIter iter;
gchar *active;
gtk_combo_box_get_active_iter(comboBox, &iter);
gtk_tree_model_get(model, &iter,
1, &active,
-1);

gtk_label_set_text(label, active);
}

int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *comboBox;
GtkCellRenderer *renderer;
GtkWidget *label;
GtkWidget *vbox;

gtk_init(&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GtkComboBox");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 50);

comboBox = gtk_combo_box_new_with_model(createModel());
gtk_combo_box_set_active(GTK_COMBO_BOX(comboBox), 0);
renderer = gtk_cell_renderer_pixbuf_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(comboBox), renderer, FALSE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(comboBox), renderer,
"pixbuf", PIXBUF_COL,
NULL);
renderer = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(comboBox), renderer, FALSE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(comboBox), renderer,
"text", TEXT_COL,
NULL);

label = gtk_label_new("caterpillar.jpg");
vbox = gtk_vbox_new(TRUE, 5);

gtk_box_pack_start(GTK_BOX(vbox), comboBox, TRUE, TRUE, 5);
gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 5);
gtk_container_add(GTK_CONTAINER(window), vbox);

g_signal_connect(GTK_OBJECT(comboBox), "changed",
G_CALLBACK(combo_changed), label);

g_signal_connect(GTK_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

gtk_widget_show_all(window);

gtk_main();

return 0;
}


The results of a line are as follows:


 



Bloggerads warning
Witness success all night
The 4-fold anti-leaking water system powers your waist and gives you a cool fit!
More messages ..
Grasp the final benefits!
Min Chen is a motorfamily
The Japanese market has appreciated. Mitsubishi will not renew this month. A car will send HTC smart phones again, and a new car will be available again. Go!
More messages ..
Impr


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.