0

I have a simple gtkmm program like that:

File main.cpp:

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

int main(int argc, char *argv[])
{
    auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

    MainWindow window;
    //Shows the window and returns when it is closed.

    return app->run(window);
}

File mainwindow.h:

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

class MainWindow : public Gtk::Window {

public:
    MainWindow();
    virtual ~MainWindow();

protected:

    Gtk::Label myLabel;
};

and file mainwindow.cpp:

#include "mainwindow.h"
#include <iostream>
//using namespace gtk;

MainWindow ::MainWindow():myLabel("this is Label")
{
add(myLabel);
show_all_children();
}
MainWindow::~MainWindow() {}

This code run ok. But now I want to declared a Label in file mainwindow.cpp like that:

#include "mainwindow.h"
#include <iostream>

MainWindow ::MainWindow():myLabel("this is Label")
{
Gtk::Label myLabel2("this is label 2");
add(myLabel2);
show_all_children();
}
MainWindow::~MainWindow() {}

The Label don't show when I run this code, can someone tell me what wrong? Thank for your help!

2 Answers 2

1

The label doesn't show up, because it's destroyed at the end of the scope (i.e. at the end of the constructor). To avoid this, you need to allocate Label on the heap. However, to avoid memory leak, you should use Gtk::manage function, so the memory of the label will be managed by the container [1].

Gtk::Label* myLabel2 = Gtk::manage(new Gtk::Label("this is label 2"));
add(myLabel2);
show_all_children();

[1] https://developer.gnome.org/gtkmm-tutorial/stable/sec-memory-widgets.html.en#memory-managed-dynamic

Sign up to request clarification or add additional context in comments.

Comments

1

You have two problems here. First myLabel2 goes out of scope have the end of your constructor and is destroyed. The second is Gtk::Window as a single item container and can only hold one widget.

The solution for myLabel2 going out of scope is to allocate it on the heap see @Marcin Kolny answer. Or construct it similar to how you have done with myLabel.

For the second issue a multi-item container needs to be added to your Gtk::Window, then you can add your other widgets to that. This container can be a Gtk::Box, Gtk::Grid, etc... It depends on your needs.

One of many possible solutions is:

mainwindow.h

#include <gtkmm.h>

class MainWindow : public Gtk::Window {
public:
    MainWindow();
    virtual ~MainWindow();
protected:
    Gtk::Box myBox;
    Gtk::Label myLabel;
    Gtk::Label myLabel2;
};

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow():
  myLabel("this is Label"), myLabel2("this is label 2");
{
  add myBox;
  myBox.pack_start(myLabel);
  myBox.pack_start(myLabel2);
  show_all_children();
}
MainWindow::~MainWindow() {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.