You are not logged in.

#1 2012-06-22 14:15:33

Hakon
Member
Registered: 2011-04-26
Posts: 10

Best practice when writing multi window gtkmm application?

Hello

Anyone here who knows gtkmm? I'm in the progress of learning it, and now I'm stuck. I can't really find a good, clean and _working_ way to add more windows.
For example I want to create a "New project" window where the user have to enter some information, and when the user hits "Create" a new main window opens.

Right now I have done it like this:
main.cpp

int main(int argc, char** argv){
	//tk_init(&argc, &argv);
	Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.control");

	NewProject newProject;
	return app->run(newProject);
}
class NewProject : public Gtk::Window{
	private:
		//Stuff for the "new_project" window
		Gtk::Label locationLabel;
		Gtk::Entry  locationEntry;
		
		Gtk::Label descriptionLabel;
		Gtk::Entry descriptionEntry;
		
		Gtk::Button createButton;
		Gtk::Grid grid;

		void CreateProject();//Callback for createButton
	public:
		NewProject();
		virtual ~NewProject();
		


};
NewProject::NewProject()
:
locationLabel("Location"),
descriptionLabel("Description and notes"),
createButton("Create")
{
	set_title("New Project");
	
	add(grid);

	//gtk_grid_set_column_spacing(GTK_GRID(grid), 5);
	//gtk_grid_set_row_spacing(GTK_GRID(grid), 5);
	grid.add(locationLabel);
	grid.add(locationEntry);
	grid.attach_next_to(descriptionLabel, locationLabel, Gtk::POS_BOTTOM, 1, 1);
	grid.attach_next_to(descriptionEntry, locationEntry, Gtk::POS_BOTTOM, 1, 1);
	grid.attach_next_to(createButton, descriptionLabel, Gtk::POS_BOTTOM, 2, 1);
	//locationEntry.show();		 
	

	createButton.signal_clicked().connect(sigc::mem_fun(*this,
				              &NewProject::CreateProject));
	
	show_all_children();
	show();
}

NewProject::~NewProject(){

}

void NewProject::CreateProject(){
	gProject.location = locationEntry.get_text();
	gProject.description = locationEntry.get_text();
	//printf(gProject.location.c_str());
	//printf(locationEntry.get_text().c_str());
	Glib::RefPtr<Gtk::Application> app = get_application();
	//std::vector<Window*> windows = app->get_windows();
	MainWindow mainWindow;
	app->add_window(mainWindow);
	//mainWindow.Create(); //Create just calls show() on the window, tried it as a simple double check, didn't work
	hide();
}

And MainWindow is defined like this:

class MainWindow : public Gtk::Window{

	private:
	 	Gtk::Button testLabel;		
	
	public:
		void Create();
		MainWindow();
		virtual ~MainWindow();
};
MainWindow::MainWindow(){
	printf(gProject.location.c_str());

	if(gProject.location.empty()){
		printf("EMPTY");
	}else{
		printf("Notempty");
	}
	set_title("asd");
	Gtk::Button testButton("TEST");
	add(testButton);
	testButton.show();
	show();	
	
	//this->project = project;

	//Show the new project window
	//Glib::RefPtr<Gtk::Application> app = get_application();
	//app->add_window(newProject);

}



MainWindow::~MainWindow(){

}

void MainWindow::Create(){

	show();
}

So, why doesn't the window show up? I know the code is executed, because I can see the printf() output in the terminal. Instead of showing the new window, the application just exits "successfully". If I open MainWindow from main, the windows shows up, but opening NewProject from MainWindow doesn't work.

As I said, I'm pretty new to GTK programming, so I am not completely sure about how I should organize the code, so what I've done here might for what I now, be completely wrong, so please guide me in the right direction smile I will take a look at other gtkmm applications to see how they have done it, but right now I have a very limited network connections, so I will have to wait until I get home in a few days.

Thank you smile I can post more code if needed, but I don't think there is any more GTK relevant code in the project.

Edit: I kind of solved this case, since I made the NewProject window as a dialoge, so it's fine for now. However, if somebody have a general solution, it would be nice smile

Last edited by Hakon (2012-06-22 20:53:42)

Offline

Board footer

Powered by FluxBB