GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Oct 20, 2014 10:48 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: A segmentation fault?
PostPosted: Mon Sep 26, 2005 7:42 pm 
Offline
Familiar Face

Joined: Thu Sep 22, 2005 12:23 pm
Posts: 20
Assignment operator for GtkWidgets?

Is there such a thing, can we do things like MyGtkWidget = SomeGtkWidget?

I get a segmentation fault when I try todo that!


What am I doing wrong?

class Data{
public:
std::string str;
GtkWidget* obj;
public:
Data(){str = "";}
void setStr(std::string ps){ str = ps;}
void setObj(GtkWidget* o){obj = o;}
};

//Declare the objects
GtkWidget* MainMenueWin;
GtkWidget* VBx;
GtkWidget* Label;
GtkWidget* Separator;
GtkWidget* btnExit;
GtkWidget* btnCreateAccount;
GtkWidget* btn2;
GtkWidget* btn3;
GtkWidget* btn4;
GtkWidget* btn5;
Data* data;
std::string msg("Under Comstruction ... I am very sorry, but this \
feature has not been implemented yet. Please \
email me for any new updates on this program \
puchica@hotmail.com");

//Callbacks

void Quit(GtkButton* , gpointer);
gboolean SystemQuit(GtkWidget*, GdkEvent*, gpointer);


void Quit(GtkButton* was_clicked, gpointer user_data){
gtk_main_quit();
}
gboolean SystemQuit(GtkWidget* widget, GdkEvent* event, gpointer user_data){
gtk_widget_destroy(widget);
gtk_main_quit();
return(TRUE);
//return(FALSE);
}

int main(int argc, char *argv[]){
// 1. Initialize the environment
gtk_set_locale ();
gtk_init(&argc, &argv);
//-liconv -liconv
// 2. Create Widgets
MainMenueWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//gtk_window_new(GTK_WINDOW_TOPLEVEL);
VBx = gtk_vbox_new(FALSE,10);
Label = gtk_label_new("Jaime");
Separator = gtk_hseparator_new();
btnCreateAccount = gtk_button_new_with_label("Create");
btn2 = gtk_button_new_with_label("Unknown");
btn3 = gtk_button_new_with_label("Unknown");
btn4 = gtk_button_new_with_label("Unknown");
btn5 = gtk_button_new_with_label("Unknown");
btnExit = gtk_button_new_with_label("Exit");
// Information for GeneralMessage


data->setObj(MainMenueWin);
data->setStr(msg);

// 3. Set attributs
gtk_window_set_title(GTK_WINDOW(MainMenueWin), "Jaime");
gtk_window_set_policy(GTK_WINDOW(MainMenueWin), //which_window
FALSE, // allow_shrink
FALSE, // allow_grow
FALSE); // auto_shrink

gtk_container_set_border_width(GTK_CONTAINER(MainMenueWin),15);
gtk_widget_set_name(MainMenueWin,"Gtk++ One, dos");
gtk_widget_set_name(VBx,"vertical box");
gtk_widget_set_name(Label,"Label");
gtk_widget_set_name(Separator, "Separator");

// 4. Register CallBacks
// These three callback fun' are only to exit the application
g_signal_connect( GTK_OBJECT(btnExit), "clicked",
G_CALLBACK(Quit), NULL);
g_signal_connect( GTK_OBJECT(MainMenueWin), "delete-event",
GTK_SIGNAL_FUNC(SystemQuit), NULL);
g_signal_connect(GTK_OBJECT(MainMenueWin), "destroy",
G_CALLBACK(SystemQuit), NULL);

g_signal_connect(GTK_OBJECT(btnCreateAccount), "clicked",
G_CALLBACK(GeneralMsg), reinterpret_cast<gpointer>(&data));

g_signal_connect(GTK_OBJECT(btn2), "clicked",
G_CALLBACK(GeneralMsg), reinterpret_cast<gpointer>(&data));
g_signal_connect(GTK_OBJECT(btn3), "clicked",
G_CALLBACK(GeneralMsg), reinterpret_cast<gpointer>(&data));
g_signal_connect(GTK_OBJECT(btn4), "clicked",
G_CALLBACK(GeneralMsg), reinterpret_cast<gpointer>(&data));
g_signal_connect(GTK_OBJECT(btn5), "clicked",
G_CALLBACK(GeneralMsg), reinterpret_cast<gpointer>(&data));

// 5. Define instance hirarchy (pack the widgets)
gtk_container_add(GTK_CONTAINER(MainMenueWin), VBx);
gtk_box_pack_start_defaults(GTK_BOX(VBx), Label);
gtk_box_pack_start(GTK_BOX(VBx), btnCreateAccount, FALSE, FALSE,1);
gtk_box_pack_start(GTK_BOX(VBx), btn2, FALSE, FALSE,1);
gtk_box_pack_start(GTK_BOX(VBx), btn3, FALSE, FALSE,1);
gtk_box_pack_start(GTK_BOX(VBx), btn4, FALSE, FALSE,1);
gtk_box_pack_start(GTK_BOX(VBx), btn5, FALSE, FALSE,1);

gtk_box_pack_start(GTK_BOX(VBx), Separator, FALSE,FALSE, 1);
gtk_box_pack_start_defaults(GTK_BOX(VBx), btnExit);


// 6. Show the Widgets
gtk_widget_show_all(MainMenueWin);

// 7. Processing loop
gtk_main();
//if (data) { delete data; }
std::cerr << "Assalam Alaykum" << std::endl;
return 0;
}

void GeneralMsg(GtkButton* button, gpointer p) {
Data* d;
GtkWidget* label;
GtkWidget* dialog;

try {
dialog = new GtkWidget;
label = new GtkWidget;
d = new Data();
} catch(std::exception& e) { std::cout << e.what() << std::endl;}
try{d = reinterpret_cast<Data*>(p);}
catch(std::exception& e){std::cerr << e.what() << std::endl;}
dialog = gtk_message_dialog_new(
einterpret_cast<GtkWindow*>(d->obj), //Parent
static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL |
GTK_DIALOG_DESTROY_WITH_PARENT), //Flags
reinterpret_cast<GtkMessageType>(GTK_MESSAGE_WARNING), // Message type
reinterpret_cast<GtkButtonsType>(GTK_BUTTONS_OK), //Button type
d->str.c_str()); //Message format

// Destroy the dialog when the user responds to it (e.g. clicks a button)


gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);

delete d;
delete dialog;
delete label;
}

_________________
* You cannot exercise your power to a point of humiliation.
- Jean Chretien

* It's amazing how the small seeds of distrust and misunderstanding
can yield a crop of hate and death...

Ma'assalama! Adiós! bye!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 9:55 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
You declared Data *data, but never initialized it. Right now it's just a pointer without love. You have to do "data = new Data();" to give it love...

Sorry for my stupidity, but I'm very tired...

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject: still, segmentation faulting
PostPosted: Wed Sep 28, 2005 2:17 pm 
Offline
Familiar Face

Joined: Thu Sep 22, 2005 12:23 pm
Posts: 20
In the constructor I assigned values to the two variables,
namespace jme{
class Data{
public:
std::string str;
GtkWidget* obj;
public:
Data(){str = ""; obj = gtk_window_new(GTK_WINDOW_TOPLEVEL);}
void setStr(std::string ps){ str = ps;}
void setObj(GtkWidget* o){obj = o;}
};
}
and as soon as I get the value for the jme::Data::str variable, I replace the value, but for jme::Data::obj, I replace the value as soon as the GtkWidget::MainMenuWin value is aquired.

But, for some reason, I am unable to use the assignment operator on GtkWidget objects.

What should I do now?

_________________
* You cannot exercise your power to a point of humiliation.
- Jean Chretien

* It's amazing how the small seeds of distrust and misunderstanding
can yield a crop of hate and death...

Ma'assalama! Adiós! bye!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group