Cet article fait parti d’un ensemble d’exemples et d’articles sur gtkmm consultables ici. Cet exemple montre comment charger et afficher dynamiquement plusieurs images:
Téléchargement

gtkmm - example 8 57.00 KB
Code source
main.cpp
#include
#include
int main(int argc, char* argv[])
{
// Initialize gtkmm
Gtk::Main app(argc, argv);
// Create the window
mainwindow w;
// Start main loop
Gtk::Main::run(w);
return 0;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#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 are clicked)
void displayImage1();
void displayImage2();
//Member widgets:
Gtk::Image image;
Gtk::Button buttonImage1;
Gtk::Button buttonImage2;
Gtk::Button buttonQuit;
Gtk::Grid mainGrid;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include
// Constructor of the main Window (build ui interface).
mainwindow::mainwindow()
{
// Initialize the main window and hide the title bar
this->set_border_width(10);
// Load and display the image
image.set("gtk.png");
mainGrid.attach(image,0,0,2,1);
// Add the button for the first image
buttonImage1.add_label("GTK");
buttonImage1.signal_pressed().connect(sigc::mem_fun(*this,&mainwindow::displayImage1));
mainGrid.attach(buttonImage1,0,1,1,1);
// Add the button for the second image
buttonImage2.add_label("MiniRex");
buttonImage2.signal_pressed().connect(sigc::mem_fun(*this,&mainwindow::displayImage2));
mainGrid.attach(buttonImage2,1,1,1,1);
// Add the Quit button
buttonQuit.add_label("Quit");
buttonQuit.signal_pressed().connect(sigc::mem_fun(*this,&mainwindow::close));
mainGrid.attach(buttonQuit,0,2,2,1);
// Display the main grid in the main window
mainGrid.show_all();
// Insert the grid in the main window
add(mainGrid);
}
// Destructor of the class
mainwindow::~mainwindow()
{}
// Display the first image
void mainwindow::displayImage1()
{
image.set("gtk.png");
}
// Display the second image
void mainwindow::displayImage2()
{
image.set("minirex.png");
}
Une réflexion au sujet de « gtkmm – exemple 8 »