Vincent
Vincent

Reputation: 51

Can't add multiple buttons to window in gtkmm?

I am still new to using widget toolkits, but I really think this should work. I copied this code from developer.gnome.org and added my own object (button2) to the Buttons class , but only the original m_button shows in the window. The contents of buttons.h:

    #ifndef GTKMM_EXAMPLE_BUTTONS_H
    #define GTKMM_EXAMPLE_BUTTONS_H

    #include <gtkmm/window.h>
    #include <gtkmm/button.h>

    class Buttons : public Gtk::Window
    {
    public:
        Buttons()
        {
        m_button.add_pixlabel("info.xpm", "hi");
        button2.add_pixlabel("info.xpm", "hello");

        set_title("Pixmap'd buttons!");
        set_border_width(10);

        m_button.signal_clicked().connect( sigc::mem_fun(*this,
          &Buttons::on_button_clicked) );

        add(button2);
        add(m_button);

        show_all_children();
        }

        virtual ~Buttons()
        {
        }

    protected:
        //Signal handlers:
        void on_button_clicked()
        {
        }

        //Child widgets:
        Gtk::Button button2;
        Gtk::Button m_button;
        };

        #endif //GTKMM_EXAMPLE_BUTTONS_H

contents of main.cpp:

    #include "buttons.h"
    #include <gtkmm/application.h>

    int main(int argc, char *argv[])
    {
    Glib::RefPtr<Gtk::Application> app =
    Gtk::Application::create(argc, argv,
    "org.gtkmm.examples.base");

    Buttons buttons;

    return app->run(buttons);
    }

Upvotes: 2

Views: 1802

Answers (1)

user140345
user140345

Reputation: 135

I'm currently also learning Gtkmm, so this might not be the best answer, but I think that the correct way of doing that would be to add Gtk::Box object and then add Gtk::Buttons to the Gtk::Box

This is how I've done it. Also I have split your header gtkmm_example_buttons.h into gtkmm_example_buttons.hpp and gtkmm_example_buttons.cpp also I've changed button name m_button to button1 to match button2 name because of the consistency.

//gtkmm_example_buttons.hpp
#pragma once //used instead of the ifdef

#include <gtkmm/window.h>
#include <gtkmm/button.h>
#include <gtkmm/box.h>

class Buttons : public Gtk::Window
{
public:
Buttons();
virtual ~Buttons();

protected:
void on_button_clicked();
Gtk::Button button1, button2;
Gtk::Box box1;
};

Also I've removed signal handling because they would just make code more complicated. You will learn more about it later.

//gtkmm_example_buttons.cpp
#include "gtkmm_example_buttons.hpp"
#include <gtkmm/window.h>
#include <gtkmm/button.h>
#include <gtkmm/box.h>

Buttons::Buttons()
{
button1.add_pixlabel("info.xpm", "hi");
button2.add_pixlabel("info.xpm", "hello");

set_title("Pixmap'd buttons!");
set_border_width(10);
add(box1);

box1.pack_start(button1);
box1.pack_start(button2);

show_all_children();
}

Buttons::~Buttons()
{
}

void Buttons::on_button_clicked()
{
}

And main:

//main.cpp
#include "gtkmm_example_buttons.hpp"
#include <gtkmm.h>

int main (int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.test");
//Shows the window and returns when it is closed.

Buttons buttons;
return app->run(buttons);
}

Upvotes: 4

Related Questions