Autore: Fabio Di Matteo
Ultima revisione: 22/04/2025 - 12:24
Visto che le cose cambiano in fretta cerchereò di tenere aggiornato un hello world in gtkmm4 pronto per un veloce copia/incolla.
Crea una classe Gtk::Window con dentro tutto quello che vogliamo metterci.
#include <gtkmm.h> class MyWindow : public Gtk::Window { public: MyWindow(); void onClick(); protected: Gtk::Grid grid; Gtk::Label mylabel; Gtk::Button mybutton; int c = 0; }; MyWindow::MyWindow() { set_title("Basic application"); set_default_size(640, 480); set_child(grid); mylabel.set_markup("<span size=\"36pt\">Hello</span>"); mybutton.set_label("Clicca"); grid.attach(mylabel, 0, 0); grid.attach(mybutton, 0, 1); mybutton.signal_clicked().connect(sigc::mem_fun(*this,&MyWindow::onClick) ); } void MyWindow::onClick() { this->c++; this->mylabel.set_markup("<span size=\"36pt\"> Cliccato: " + std::to_string(this->c) + "</span>"); } int main(int argc, char* argv[]) { auto app = Gtk::Application::create("org.gtkmm.examples.base"); return app->make_window_and_run<MyWindow>(argc, argv); } /* * Compilare con g++ main.cc -o simple `pkg-config --cflags --libs gtkmm-4.0` -std=c++17 * */
Crea una classe finestra vuota. Dopo carica dal file Gtk::builder il contenitore principale (in questo caso una Gtk::Grid) con dentro tutti i widget. Viene ignorata la finestra del file Gtk::Builder perchè non serve. Il file dell'interfaccia grafica Gtk::Builder è stato disegnato con Cambalache.
#include <gtkmm.h> class MyWindow : public Gtk::Window { public: MyWindow(); void onClick(); protected: int c = 0; // Da notare che quando si usa Gtk::Builder i vari oggetti grafici sono puntatori. Gtk::Grid *mygrid; // Contenitore principale per adesso vuoto vuoto. Gtk::Label *mylabel; Gtk::Button *mybutton; }; MyWindow::MyWindow() { //Dal file gtkbuilder preleviamo solo il widget contenitore mygrid, ignorando la finestra. auto builder = Gtk::Builder::create_from_file("simple.ui", "mygrid"); mygrid = builder->get_widget<Gtk::Grid>("mygrid"); mybutton = builder->get_widget<Gtk::Button>("mybutton"); mylabel= builder->get_widget<Gtk::Label>("mylabel"); mylabel->set_markup("<span size=\"36pt\">Clicca sul bottone</span>"); set_child(*mygrid); set_title("Basic application gtk builder"); set_default_size(440, 180); mybutton->signal_clicked().connect(sigc::mem_fun(*this,&MyWindow::onClick) ); } void MyWindow::onClick() { this->c++; this->mylabel->set_markup("<span size=\"36pt\"> Cliccato: " + std::to_string(this->c) + "</span>"); } int main(int argc, char* argv[]) { auto app = Gtk::Application::create("org.gtkmm.examples.gtkbuilder"); return app->make_window_and_run<MyWindow>(argc, argv); } /* * Il file contenente l'interfaccia grafica (simple.ui) è stato realizzato con Cambalache. * Compilare con g++ main.cc -o simple `pkg-config --cflags --libs gtkmm-4.0` -std=c++17 * */
Il nostro wiki installa solamente cookie tecnici necessari al funzionamento della piattaforma "Dokuwiki". Niente analitics, statistiche, tracciamenti o altro.