PDA

View Full Version : [SOLVED] Gtkmm: CheckButton won't set?!



fallenshadow
November 1st, 2012, 11:53 AM
Can't someone explain why on earth this won't set the checkButton?



Gtk::CheckButton *checkbutton;
refBuilder->get_widget("maximize", checkbutton);
checkbutton->set_active(true);

Its driving me insane at this point! :mad:

spjackson
November 1st, 2012, 12:26 PM
Well, that looks fine to me, in principle. Some wild guesses... Maybe get_widget isn't getting the widget you think it is. Or maybe you have some other code that calls set_active(false), or in some other way overrides what is happening here before the screen gets painted.

fallenshadow
November 1st, 2012, 12:54 PM
Here is some more complete code:



getline (myfile,line);
cout << line << endl;

if(line.compare("maximize=1")==0)
{
cout << "Maximize checked" << endl;
Gtk::CheckButton *checkbutton;
refBuilder->get_widget("maximize", checkbutton);
checkbutton->set_active(true);
bool maxStatus = checkbutton->get_active();
cout << maxStatus << endl;
}


Output:


maximize=1
Maximize checked
1


So the comparison is successful with the value read in from the file. Somehow it just won't set the checkButton. It is not being set to false from anywhere else. :/

SledgeHammer_999
November 1st, 2012, 02:18 PM
Can you post the glade file? Are you sure that "maximize" identifies a checkbutton?

fallenshadow
November 1st, 2012, 02:53 PM
Yes it does! No need to post the full file, I did all the basic checks already.

http://i47.tinypic.com/5f5d15.png

fallenshadow
November 5th, 2012, 08:24 PM
My code snippet is from around line 60 on this file:

http://bazaar.launchpad.net/~dylan-coakley/photofiltre-lx/main/view/head:/src/prefs.cc

(but the problem might be elsewhere in this file or in my mainwindow file.

Any help on the matter is appreciated! :)

SuperCamel
November 6th, 2012, 01:30 AM
I'm just perusing your code. I recommend checking for NULL pointers after calling get_widget(), just to protect against potential seg faults.

I can't see anything that would cause your problem though. Perhaps you could post the entire project?

fallenshadow
November 6th, 2012, 10:46 AM
The whole of my current codebase can be downloaded here:

http://bazaar.launchpad.net/~dylan-coakley/photofiltre-lx/main/tarball/125

Compile using the make system or using the simple compile script in the src folder.

To test:

Go to Tools>>Preferences. Click Startup tab. Click the "Maximize on startup" checkButton. The click the Ok button to confirm.

The restart the app. The checkButton is supposed to be active but it is not for some unknown reason.

SuperCamel
November 6th, 2012, 12:03 PM
Got it!


while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
if(line.compare("maximize=1")==0)
{
cout << "Maximize checked" << endl;
Gtk::CheckButton *checkbutton;
refBuilder->get_widget("maximize", checkbutton);
checkbutton->set_active(true);
bool maxStatus = checkbutton->get_active();
cout << maxStatus << endl;
}
if(line.compare("maximize=0")==0) //this was else if(line.compare("maximize=0")!=0)

//that meant any line read in that was NOT "maximise=1" would uncheck the button.
{
//maximize unchecked
cout << "Maximize unchecked" << endl;
Gtk::CheckButton *checkbutton;
refBuilder->get_widget("maximize", checkbutton);
checkbutton->set_active(false);
}
}

SuperCamel
November 6th, 2012, 12:08 PM
Also, does your prefsDialog class inherit Gtk::Dialog for any reason?

I think you may want to check out Gtk::Builder::get_widget_derived(). It allows you to create a class that inherits a widget, and then load the widget from a glade file. It's very useful.

fallenshadow
November 6th, 2012, 12:40 PM
Awesome! :cool: You just saved me from insanity! :o

Funnily enough this was the first feature I had working. I developed further features and then replaced the version on my Launchpad. However, I never noticed this bug until now!

This shall only remind me to test more often in case I go breaking things in my code without realising it! :)

My preferences inherits Gtk:: Dialog because its a dialog I guess. Thats about the only reason for doing it! :D

I will check out Gtk::Builder::get_widget_derived(). Thanks for the tip!