gtkmm – example 24

This is part of a more general work dedicated to gtkmm accessible here. This example shows how to create an application with Tabs. The first tab display a button and the second tab display a label.

capture

Download


Source code


main.cpp

#include 
#include 


int main(int argc, char* argv[])
{
    // Initialize gtkmm and create the main window
    Glib::RefPtr app = Gtk::Application::create(argc, argv, "www.lucidarme.me");
    // Create the window
    mainwindow window;
    // Start main loop
    return app->run(window);
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 




// The class mainwindow inherits from Gtk::Window
class mainwindow : public Gtk::Window
{
    // Constructor and destructor
public:    
	mainwindow();
	virtual             ~mainwindow();

protected:
    //Signal handlers (run when the button is clicked)
    void                    on_button_clicked();

    //Page selector
    Gtk::Notebook           Selector;

    // Button for the first page
    Gtk::Button             Button;

    // Label for the second page
    Gtk::Label              Label;
};


#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"



// Constructor of the main Window (build ui interface).
mainwindow::mainwindow()
{
    // Resize the main window and set border width
    this->resize(400,200);
    set_border_width(5);

    // Add the label and connect the button
    Button.add_label("Page 1 (button)");
    Button.signal_clicked().connect(sigc::mem_fun(*this,&mainwindow::on_button_clicked));

    // Set label text
    Label.set_text("Page 2 (label)");

    // Add the widgets (button and label) in the notebook
    Selector.append_page(Button, "First");
    Selector.append_page(Label, "Second");

    // This packs the button into the Window (a container).
    add(Selector);
    show_all_children();
}




// Destructor of the class
mainwindow::~mainwindow()
{}

// Call when the button os clicked and display my url
void mainwindow::on_button_clicked()
{
    std::cout << "www.lucidarme.me" << std::endl;
}

Leave a Reply

Your email address will not be published. Required fields are marked *