LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   gtkmm: show() doesn't show the window when adding windows from other places than main() (http://www.linuxquestions.org/questions/programming-9/gtkmm-show-doesnt-show-the-window-when-adding-windows-from-other-places-than-main-4175412677/)

Hvl 06-21-2012 09:54 AM

gtkmm: show() doesn't show the window when adding windows from other places than main()
 
Hello

Anyone here who knows gtkmm? I'm in the progress of learning it, and now I'm stuck. What I want to do is to first create a window, for example a typical "New Project" window. This New Project window is added from main, like this:
main.cpp
Code:

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);
}

       
Code:

       
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:
Code:

class MainWindow : public Gtk::Window{

        private:
                Gtk::Button testLabel;               
       
        public:
                void Create();
                MainWindow();
                virtual ~MainWindow();
};

Code:

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 the 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.

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


All times are GMT -5. The time now is 03:40 PM.