Frequently Asked Questions

1. gtkmm's place in the world

What is GTK?

GTK is the GUI widget toolkit, written in C, which serves as the foundation for the GNOME project as well as many stand-alone applications. GTK is the foundation on which gtkmm is built. See https://www.gtk.org.

Why is it named gtkmm?

gtkmm was originally named gtk-- because GTK was originally named GTK+ and had a + in the name. However, as -- is not easily indexed by search engines, the package generally went by the name gtkmm, and that's what we stuck with.

Why use gtkmm instead of GTK?

  • gtkmm allows you to write code using normal C++ techniques such as encapsulation, derivation, and polymorphism. As a C++ programmer you probably already realize that this leads to clearer and better organized code.
  • gtkmm is more type-safe, so the compiler can detect errors that would only be detected at run-time when using C. This use of specific types also makes the API clearer because you can see what types should be used just by looking at a method's declaration.
  • Inheritance can be used to derive new widgets. The derivation of new widgets in GTK C code is so complicated and error prone that almost no C coders do it. As a C++ developer you know that derivation is an essential Object Orientated technique.
  • Member instances can be used, simplifying memory management. All GTK C widgets are dealt with by use of pointers. As a C++ coder you know that pointers should be avoided where possible.
  • Less code. The GTK C object model uses prefixed function names and cast macros. For instance: gtk_button_set_text(GTK_BUTTON(button), "sometext"); gtkmm C++ code is shorter and clearer. For instance: button.set_text("sometext");
  • There's no need to worry about GTK's reference-counting policy.

Why use libsigc++? Why not use the GTK signal functions?

  • GTK signals aren't typesafe. The compiler can't tell you whether your callback has the wrong number or type of arguments or return value.
  • They can only be used with functions or static methods. With libsigc++ callbacks can also be instance methods, using the member data of a particular object. They can also be virtual methods which you could override in a derived class.

Why isn't GTK/GNOME itself written in C++?

  • C is a simpler language so more people are familiar with it, particularly on Unix.
  • C can be wrapped by any other language, making the API available to more developers.
  • GTK and GNOME have very well organized C code, much more sane than most of the C code that we encounter. This is partly due to its C-based object-orientated structure.

Why not use Qt if you like C++ so much?

gtkmm developers tend to prefer gtkmm to Qt because gtkmm does things in a more C++ way. Qt originates from a time when C++ and the standard library were not standardized or well supported by compilers. It therefore duplicates a lot of stuff that is now in the standard library, such as containers and type information. Most significantly, they modified the C++ language to provide signals, so that Qt classes cannot be used easily with non-Qt classes. gtkmm was able to use standard C++ to provide signals without changing the C++ language.

Also, gtkmm and the other *mm modules allow you to build software which works more closely with the GNOME desktop.

2. How good is gtkmm?

What systems does it run under?

gtkmm should run under any UNIX-type system with the proper compilers and libraries installed. The GNU C++ compiler (g++, part of gcc) together with the GNU toolset (such as found on Linux and *BSD systems) comprise its default build environment. It can also be built and used on Windows with the mingw build tools or MSVC.

How complete is it?

gtkmm tries to offer all of the functionality offered by GTK. This means that you should be able to do anything with gtkmm that's supported by GTK, and do it more easily. If something isn't covered then we want to know about it.

GLib contains classes with similar functionality as standard C++ classes, for instance GThread and GTree. Such GLib classes are not wrapped in glibmm.

Does gtkmm use Standard C++ containers such as std::string and std::vector?

Yes, we believe in reusing standard C++ code wherever possible. This might not be obvious at first because gtkmm has Glib::ustring which has almost the same interface as std::string. Glib::ustring exists because the C++ standard does not support UTF8-encoded strings well.

How does gtkmm compare to Qt?

  • gtkmm uses pure C++. Qt requires extensions to C++ that are parsed by the moc pre-processor.
  • gtkmm uses std::string, std::vector, iterators, etc. Qt has its own Qt-specific containers.
  • With gtkmm normal C++ memory management can be used. Qt demands that all widgets are dealt with as pointers, and that deletion of widgets is delegated to parent widgets.
  • Arrangement of widgets seems to be easier in gtkmm. In Qt, Containers and Layouts are separate classes, and child widgets must be added to both.
  • The gtkmm API tends to be more explicit. The behavior of Qt classes is often dependent upon the implicit effects of overridden constructors.

3. Further information

Onde é possível discutir sobre gtkmm?

See the Discussion page.

What documentation is there for gtkmm?

See the Documentation page.

Where can I find some example code?

See the examples directory in the gtkmm-documentation module. Most of these appear in the gtkmm book.

4. Using gtkmm

What compiler arguments should I use to compile a gtkmm program?

See the reference documentation.

How can I get the GTK object from a gtkmm object?

If you need some GTK functionality which is not supported through gtkmm, you can call the gobj() method in the relevant class, which will return a pointer to the plain C GTK object. You can then operate directly on this C object as you would in any GTK program.

How can I wrap a GTK widget in a gtkmm instance?

Glib::wrap() will give you a pointer to a gtkmm object. It is an overloaded function, so it will give you an instance of the appropriate class.

Can I use C++ exceptions with gtkmm?

Yes, but there are restrictions. Since plain C doesn't know what a C++ exception is, you can use exceptions in your gtkmm code as long as there are no C functions in your call stack between the thrower and the catcher. This means that you have to catch your exception locally.

You will be warned at runtime about uncaught exceptions, and you can specify a different handler for uncaught exceptions. Some gtkmm methods use exceptions to report errors. The exception types that might be thrown are listed in the reference documentation of these methods.

How can I use Cambalache or Glade with gtkmm?

See the Gtk::Builder chapter in the gtkmm book. Cambalache replaces the Glade application. Glade can't be used with GTK4/gtkmm4.

What does Gtk::make_managed<T>(args) do?

This means The container widget will delete this child widget. Use it if you don't want to worry about when to delete dynamically allocated widgets.

How can I learn about arranging widgets? I am confused by the packing options.

Cambalache is a great way to see what can be done with GTK and GNOME widgets. Use Cambalache to explore the choice of widgets and to see how they can be put together.

I'm used to MFC (Microsoft Foundation Class Library). Where is the Document and the View?

Document/View (which is a version of the Model–View–Controller (MVC) software design pattern) is not supported directly by GTK. However, the Gtk::TextView and Gtk::ListView classes are split up into model and view.

How do I load pictures for use with gtkmm?

Use Gdk::Pixbuf and/or Gtk::Image. Both are easy to use and support a wide range of image file types.

Is gtkmm thread-safe?

Neither GTK nor gtkmm are thread-safe. All GUI code must run in the same thread. If your program might benefit from using multiple threads, have a look at the Multi-threaded programs chapter in the gtkmm book.