GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

combo box

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
alen121
Familiar Face


Joined: 28 Sep 2007
Posts: 30

PostPosted: Wed Feb 06, 2008 9:28 am    Post subject: combo box Reply with quote

Hi . I want to add 2 items to my combobox but there is no display . what is the problem ?
Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <gtk/gtk.h>


int main(int argc, char *argv[]) {
    GtkWidget *window , *combo;
 gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
combo=gtk_combo_box_new();
GtkListStore *mylist ;
GtkTreeIter iter ;
mylist=gtk_list_store_new(1,G_TYPE_STRING);
gtk_list_store_append(mylist, &iter);
gtk_list_store_set (mylist, &iter,0,"Residental", -1);
gtk_list_store_append(mylist, &iter);
gtk_list_store_set (mylist, &iter,0,"Commerical", -1);
gtk_combo_box_set_model(GTK_COMBO_BOX(combo),GTK_TREE_MODEL(mylist));
gtk_container_add (GTK_CONTAINER (window),combo);
gtk_widget_show_all(window);
gtk_main();
return 0 ;
}
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 480
Location: Portland, OR USA

PostPosted: Tue Feb 12, 2008 5:53 pm    Post subject: Reply with quote

If all you're looking to do is add text to the combo box, perhaps it would be simpler to use gtk_combo_box_new_text () and gtk_combo_box_append_text () functions instead of your own model.

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <gtk/gtk.h>

int main(int argc, char *argv[]) {

    GtkWidget *window , *combo;
   
    gtk_init(&argc, &argv);
   
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    combo=gtk_combo_box_new_text();
   
    gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "Residental");
    gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "Commerical");
    gtk_container_add (GTK_CONTAINER (window),combo);
    gtk_widget_show_all(window);

    g_signal_connect (G_OBJECT(window), "destroy",
                        G_CALLBACK (gtk_main_quit), NULL);
                       
    gtk_main();
    return 0 ;
}
Back to top
Lino
Familiar Face


Joined: 27 Dec 2007
Posts: 20

PostPosted: Sun Mar 09, 2008 6:52 pm    Post subject: Reply with quote

Can I create a GtkComboBox with a custom GtkTreeModel? Yes, but how I add columns and cell-renderer?
Back to top
tadeboro
Familiar Face


Joined: 23 Jul 2008
Posts: 7

PostPosted: Wed Jul 23, 2008 10:37 am    Post subject: Reply with quote

Helo

In order to pack more than one element into the combo box, you need to use cell layout interface.

Below is a simple example with 2 elements embedded in combo box.

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
GtkWidget *combo;
GtkCellRenderer *renderer;
GtkListStore *model;
GtkTreeIter iter;


/* Create model for combo */
model = gtk_list_store_new( 2, G_TYPE_BOOLEAN,
                               G_TYPE_STRING );
gtk_list_store_append( model, &iter );
gtk_list_store_set( model, &iter,
                    0, FALSE,
                    1, "Pathological function",
                   -1 );

gtk_list_store_append( model, &iter );
gtk_list_store_set( model, &iter,
                    0, FALSE,
                    1, "Physiological function",
                   -1 );

/* create combo box */
combo = gtk_combo_box_new_with_model(
            GTK_TREE_MODEL( model ) );
/* place combo somewhere */

/* create columns */
renderer = gtk_cell_renderer_toggle_new();
gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( combo ),
                            renderer, FALSE );
gtk_cell_layout( GTK_CELL_LAYOUT( combo ), renderer,
                 "active", 0,
                 NULL );

renderer = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( combo ),
                            renderer, TRUE );
gtk_cell_layout( GTK_CELL_LAYOUT( combo ), renderer,
                 "text", 1,
                 NULL );


Hope it helps.

Tadej
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP