Jacob Garby
Jacob Garby

Reputation: 855

Open multiple different windows in Gtkmm

I've only just started learning how to use gtkmm, and I'm trying to create an application which can have more than one window open at the same time (think, main window and a control panel).

I decided to create the layout in Glade, if that's of any relevance to this question.

My method of displaying two bottles is more or less this:

Gtk::Window* main_window = nullptr;
Gtk::Window* servsettings = nullptr;

int main(int argc, char* argv[]) {
    auto app = ...
    auto builder = ... // these are exluded for brevity

    builder->add_from_file("../src/design.glade");

    builder->get_widget("main", main_window);
    builder->get_widget("servsettings", servsettings);

    app->run(*servsettings);
    app->run(*main_window);
}

Instead of opening two windows, this instead opens servsettings, then segfaults when I close that.

Now forgetting about the segfault (I'm utterly confused about that,) I think I can see why it only opens servsettings - I assume this is because I'm running the app, and then that call only exits when the window dies?

The problem is, I can't think of any other way to do it. I experimented with multithreading but decided that it would be better to ask here first.

Before anyone suggests it, this answer does not help me. This is because they had a scope-based issue. I don't.

Upvotes: 0

Views: 1856

Answers (2)

pan-mroku
pan-mroku

Reputation: 891

Use Gtk::Application::add_window(Gtk::Window&).

If all the windows managed by Gtk::Application are closed (hidden) or removed from the application then the call to run() will return.

#include <gtkmm.h>

Gtk::Window* window1, *window2;

int main()
{
    auto app = Gtk::Application::create();
    Gtk::Button button1("Quit"), button2("Quit");
    window1 = new Gtk::Window();
    window2 = new Gtk::Window();
    button1.signal_clicked().connect(sigc::mem_fun(window1, &Gtk::Window::close));
    button2.signal_clicked().connect(sigc::mem_fun(window2, &Gtk::Window::close));

    window1->set_default_size(200, 200);
    window1->add(button1);
    window1->show_all();

    window2->set_default_size(200, 200);
    window2->add(button2);
    window2->show_all();

    app->signal_startup().connect([&]{
            app->add_window(*window2);
            });
    return app->run(*window1);
}

Upvotes: 0

Jacob Garby
Jacob Garby

Reputation: 855

I found the answer. For those wondering, I replaced the two app->runs with:

servsettings->show();
main_window->show();
app->run(*main_window);

Upvotes: 0

Related Questions