ECJones
ECJones

Reputation: 21

Gtkmm Window is blank, not showing any widgets or title

I'm learning gtkmm in order to program Conway's Game of Life as a demo. Currently I'm trying to show two buttons in a header bar, and I'm following a tutorial, but nothing is showing up in the window. Here's my code:

Display.h:

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

class Display : public Gtk::Window
{
public:
    Display();
    Display(int xSize, int ySize);
    virtual ~Display();

private:
    //child widgets
    Gtk::HeaderBar mHeader;
    Gtk::Button startButton;
    Gtk::Button stopButton;

};

Display.cpp:

#include "Display.h"

Display::Display(int xSize, int ySize):
startButton("start"),
stopButton("stop"),
mHeader()
{
    //set window properties
    set_title("Conway's Game of Life");
    set_size_request(xSize, ySize);
    set_border_width(5);
    mHeader.set_title("Game of Life");

    //add to header bar
    mHeader.pack_start(startButton);
    mHeader.pack_start(stopButton);

    //add header bar
    add(mHeader);

    //make everything visible
    show_all();


}

Display::Display()
{
    Display(600, 600);
}

Display::~Display() {}

Main.cpp:

#include "Display.h"
#include <gtkmm.h>

int main(int argc, char **argv)
{
    auto app = Gtk::Application::create(argc, argv);

    Display Window; 

    return app->run(Window);
}   

I've been trying to fix this for quite a while and can't seem to figure it out. Any help would be greatly appreciated.

Upvotes: 0

Views: 359

Answers (1)

BobMorane
BobMorane

Reputation: 4296

The problem is that you are not using constructor delegation correctly. Try to write your default constructor as follow instead:

Display::Display()
: Display(600, 600) // Delegate here, not in body...
{

}

and it should work. Note that this is a C++11 feature.

Upvotes: 1

Related Questions