GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Jul 25, 2014 1:35 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Gtk-CRITICAL **: gtk_container_add: assertion `GTK_ ........
PostPosted: Wed Jul 03, 2013 1:47 pm 
Offline

Joined: Wed Jul 03, 2013 1:31 pm
Posts: 4
Hello
I just started learning GTK+ today, after a year of teaching programming in C, and I have gotten this error, in the course of running the program like 3 times. I have difficulties proceeding.
Gtk-CRITICAL **: gtk_container_add: assertion `GTK_IS_CONTAINER (container)' failed

The following is an example program
Code:
#include<stdio.h>
#include<gtk/gtk.h>
#include<string.h>
int main(int argc, char * argv[]){
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *label1, *label2, *label3, *box1;
box1 = gtk_box_new(FALSE, 5);
gtk_container_add(GTK_CONTAINER(window), box1);
label1 = gtk_label_new("Testing1");
label2 = gtk_label_new("Testing2");
label3 = gtk_label_new("Testing3");
gtk_box_pack_start(GTK_BOX(box1), label1, TRUE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(box1), label2, TRUE, FALSE, 5);
gtk_widget_show_all(window);
gtk_main();
return 0;
}


Top
 Profile  
 
 Post subject: Re: Gtk-CRITICAL **: gtk_container_add: assertion `GTK_ ....
PostPosted: Wed Jul 03, 2013 2:04 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 733
Location: UK
Hello,

Your example is incomplete as you never create a window and use some of the GTK API incorrectly. A corrected version is shown below. Note there is no connection of signals.

Code:
#include <gtk/gtk.h>

int main(int argc, char * argv[])
{
    GtkWidget *window;
    GtkWidget *label1, *label2, *label3, *box1;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    box1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_container_add(GTK_CONTAINER(window), box1);

    label1 = gtk_label_new("Testing1");
    label2 = gtk_label_new("Testing2");
    label3 = gtk_label_new("Testing3");

    gtk_box_pack_start(GTK_BOX(box1), label1, TRUE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(box1), label2, TRUE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(box1), label3, TRUE, FALSE, 5);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

_________________
E.


Top
 Profile  
 
 Post subject: Re: Gtk-CRITICAL **: gtk_container_add: assertion `GTK_ ....
PostPosted: Thu Jul 04, 2013 5:32 am 
Offline

Joined: Wed Jul 03, 2013 1:31 pm
Posts: 4
Thank you indeed errol. I have gotten it running, though with some modifications like the use of gtk_vbox_new and gtk_hbox_new. I have gotten it running, now struggling to get an event to call another window. Down is my example

Code:
#include<gtk/gtk.h>
void closeApp(GtkWidget *widget, gpointer data){
gtk_main_quit();
}
int main(int argc, char *argv[]){
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *usernamelabel, *usernameentry,*passwordlabel,*passwordentry, *ok_button;
GtkWidget *box1, *box2, *box3, *box4;
//creating a window
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 300,200);
gtk_window_set_title(GTK_WINDOW(window),"Our Window");

g_signal_connect(G_OBJECT(window), "distroy", G_CALLBACK(closeApp), NULL);
//labels creation
usernamelabel = gtk_label_new("Username");
usernameentry = gtk_entry_new();
passwordlabel = gtk_label_new("Password");
passwordentry = gtk_entry_new();
ok_button = gtk_button_new_with_label("OK");
//boxes
box1 = gtk_hbox_new(TRUE, 5);
box2 = gtk_hbox_new(TRUE, 5);
box3 = gtk_hbox_new(TRUE, 5);
box4 = gtk_vbox_new(FALSE, 5);
//packing them
gtk_box_pack_start(GTK_BOX(box1), usernamelabel, TRUE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(box1), usernameentry, TRUE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(box2), passwordlabel, TRUE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(box2), passwordentry, TRUE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(box3), ok_button, TRUE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(box4), box1, FALSE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(box4), box2, FALSE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(box4), box3, FALSE, FALSE, 5);
//Into the container
gtk_container_add(GTK_CONTAINER(window), box4);
gtk_widget_show_all(window);
gtk_main();
return 0;
}

Thanks Indeed


Top
 Profile  
 
 Post subject: Re: Gtk-CRITICAL **: gtk_container_add: assertion `GTK_ ....
PostPosted: Thu Jul 04, 2013 6:06 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 733
Location: UK
Just a few notes first. When posting a section of code please use the "Code" tags and do not just change the colour. If you can also layout your code so that it is easy to read ifor example put indentation instead of everything on one level would help everybody.

You have never said which version of GTK+ you are using. In GTK+ v3.2 and above GtkHBox and GtkVBox have both been deprecated and should not be used.

Quote:
now struggling to get an event to call another window.
Can you explain more what this means.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Gtk-CRITICAL **: gtk_container_add: assertion `GTK_ ....
PostPosted: Thu Jul 04, 2013 11:00 am 
Offline

Joined: Wed Jul 03, 2013 1:31 pm
Posts: 4
Again, thanks, I will abide by the convetions from now on.
What I meant is to have another window, beside this main window, given some input by the users. I am using gtk+ version 3, from my compilation `pkg-config --cflags --libs gtk+-3.0' after the normal gcc


Top
 Profile  
 
 Post subject: Re: Gtk-CRITICAL **: gtk_container_add: assertion `GTK_ ....
PostPosted: Thu Jul 04, 2013 6:00 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 733
Location: UK
What you need to do is create another window and its contents and show it to the user. You will also need to connect the signals and keep track of the window.

Be careful about the use of local variables as these will not be accessible once the call back function has exited.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group