Search code examples
c++buttongtkshowgtkmm

Program in gtkmm will not show buttons


I am trying to write a program in gtkmm but the buttons will not show up. I've done everything I know to make these buttons show, but nothing has been working. I have even included the 'show all' methods in both the main and win_home.cpp files but still nothing happens. The program DOES however go through the code, as the cout statements are all being printed. Does anyone have any idea why these buttons would not be showing up?

main.cpp:

#include <gtkmm.h>
#include <iostream>
#include "win_home.h"

int main(int argc, char *argv[])
{
    auto app = Gtk::Application::create(argc, argv, "com.InIT.InITPortal");

    std::cout << "Creating Portal Window" << std::endl;
    HomeGUI win_home;

    win_home.set_default_size(600,400);
    win_home.set_title("St. George InIT Home");

    return app->run(win_home);
}

win_home.cpp:

#include "win_home.h"

HomeGUI::HomeGUI()
{
    //build interface/gui
    this->buildInterface();
    //show_all_children();

    //register Handlers
    //this->registerHandlers();
}
HomeGUI::~HomeGUI()
{

}

void HomeGUI::buildInterface()
{

    std::cout << "Building Portal Interface" << std::endl;
    m_portal_rowbox = Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 5);
    add(m_portal_rowbox);
        Gtk::Button m_pia_button = Gtk::Button("Printer Install Assistant");
            m_portal_rowbox.pack_start(m_pia_button, false, false, 0);
            m_pia_button.show();
        Gtk::Button m_inventory_button = Gtk::Button("Inventory");
        m_inventory_button.show();
            m_portal_rowbox.pack_start(m_inventory_button, false, false, 0);
            m_inventory_button.show();

    //add(m_portal_rowbox);
    //m_portal_rowbox.show_all();
    m_portal_rowbox.show();
    this->show_all_children();
    std::cout << "Completed Portal Interface" << std::endl;

    return;
}

void HomeGUI::registerHandlers()
{

}

Solution

  • In void HomeGUI::buildInterface() you have constructed 2 buttons and they are added it to your box container. When the function returns the buttons are destroyed as they are now out of scope. Since they no longer exist they can not be visible.

    So for you first button you would use something like this:

    Gtk::Button * m_pia_button = Gtk::manage(
        new Gtk::Button("Printer Install Assistant"));
    m_portal_rowbox.pack_start(&m_pia_button, false, false, 0);
        m_pia_button.show();
    

    I expect you would need easy access to your buttons throughout the life time of your window. The easiest way is to have the buttons as a member of your class. It will be constructed as an empty button and you just need to set the label afterwards.

    class HomeGUI {
      ....
      // A button (empty)
      Gtk::Button m_pia_button;
      ....
    };
    ....
    void HomeGUI::buildInterface()
    {
      ....
      m_pia_button.set_label("Printer Install Assistant");
      m_portal_rowbox.pack_start(m_pia_button, false, false, 0);
            m_pia_button.show();
      ....
    }