User Tools

Site Tools


programming:gtk4

This is an old revision of the document!


Gtk4

Gtk (source) is a GUI cross-platform toolkit.

Build from source

build_gtk4_notes.txt
# Install
sudo apt install ninja-build
sudo apt install meson # or `pip install meson` if too old
 
# You might find along the way you need some other dependencies
sudo apt install libgstreamer-plugins-bad1.0-dev
 
# Need to build Glib first:
# https://download.gnome.org/sources/glib/2.71/glib-2.71.2.tar.xz
# In glib2 source dir:
meson setup builddir
meson compile -c builddir
meson install -c builddir
 
# Need to build Glib-networking to get TLS
# https://download.gnome.org/sources/glib-networking/2.70/
# sudo apt install gsettings-desktop-schemas-dev
# sudo apt install libproxy-dev
# sudo apt install cmake
# In the glib-networking dir:
meson setup builddir
cd builddir
ninja
ninja install
 
# For GTK4, in Gtk4 source dir
# Get build options:
meson configure
 
# To build with `gtk_doc=true` option
pip install gi-docgen 
sudo apt install gir1.2-graphene-1.0 libgraphene-1.0-dev
 
# Omit prefix to let it install to install to system `/usr/local/`
meson setup -Dgtk_doc=true --prefix $HOME/gtk4 builddir
 
# Might need to install some deps like libjpeg,  or sudo apt install libgstreamer1.0-dev
 
cd builddir
ninja
 
# Documentation will now be in docs/
# For example: gtk-4.6.1/docs/reference/gtk/gtk4/index.html
 
# There are examples in source under examples/ directory 
 
# Install to prefix (use `sudo` if doing system install)
ninja install

Documentation

Offline

There is example code in the examples/ directory of the source. If you built from source and included the docs with the build, there will also be nice HTML pages in the docs/ directory.

  • API documentation - index.html - e.g. gtk-4.6.1/docs/reference/gtk/gtk4/index.html
  • Widget gallery: visual_index.html - e.g. gtk-4.6.1/docs/reference/gtk/gtk4/visual_index.html
  • Example code: examples/ directory - e.g. gtk-4.6.1/examples/

Online

Hello world

Example Makefile:

Makefile
all: main
 
main: main.c
	gcc `pkg-config --cflags gtk4` main.c `pkg-config --libs gtk4`
 
clean:
	rm a.out
main.c
// From https://www.gtk.org/
#include <gtk/gtk.h>
 
static void on_activate (GtkApplication *app) {
  // Create a new window
  GtkWidget *window = gtk_application_window_new (app);
  // Create a new button
  GtkWidget *button = gtk_button_new_with_label ("Hello, World!");
  // When the button is clicked, close the window passed as an argument
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_close), window);
  gtk_window_set_child (GTK_WINDOW (window), button);
  gtk_window_present (GTK_WINDOW (window));
}
 
int main (int argc, char *argv[]) {
  // Create a new application
  GtkApplication *app = gtk_application_new ("com.example.GtkApplication",
                                             G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
  return g_application_run (G_APPLICATION (app), argc, argv);
}

gtkmm (C++)

You can use gtkmm to write C++ code instead of C. On their wiki they have tutorials and build instructions for the source code.

programming/gtk4.1647803546.txt.gz · Last modified: 2022/03/20 19:12 by nanodano