Common QuestionsCommon Questions — Find answers to common questions in the GTK+ manual |
Questions and Answers
This is an "index" of the reference manual organized by common "How do I..." questions. If you aren't sure which documentation to read for the question you have, this list is a good place to start.
1. General | ||||||
1.1. | How do I get started with GTK+? | |||||
The GTK+ website offers a tutorial and a FAQ . More documentation ranging from whitepapers to online books can be found at the GNOME developer's site . After studying these materials you should be well prepared to come back to this reference manual for details. | ||||||
1.2. | Where can I get help with GTK+, submit a bug report, or make a feature request? | |||||
See the documentation on this topic . | ||||||
1.3. | How do I port from one GTK+ version to another? | |||||
See the list of incompatible changes from 1.2 to 2.0 . Also, the GNOME 2.0 porting guide on http://developer.gnome.org has some more detailed discussion of porting from 1.2 to 2.0. You may also find useful information in the documentation for specific widgets and functions. If you have a question not covered in the manual, feel free to ask on the mailing lists and please file a bug report against the documentation. | ||||||
1.4. | How does memory management work in GTK+? Should I free data returned from functions? | |||||
See the documentation for GObject and GtkObject . For GObject note specifically and . GtkObject is a subclass of GObject so the same points apply, except that it has a "floating" state (explained in its documentation). For strings returned from functions, they will be declared "const" (using G_CONST_RETURN ) if they should not be freed. Non-const strings should be freed with . Arrays follow the same rule. (If you find an exception to the rules, please report a bug to http://bugzilla.gnome.org .) | ||||||
1.5. | Why does my program leak memory, if I destroy a widget immediately after creating it ? | |||||
If GtkFoo isn't a toplevel window, then is a memory leak, because no one assumed the initial floating reference. If you are using a widget and you aren't immediately packing it into a container, then you probably want standard reference counting, not floating reference counting. To to get this, you must acquire a reference to the widget and drop the floating reference (“ref and sink ” in GTK+ parlance) after creating it: When you want to get rid of the widget, you must call to break any external connections to the widget before dropping your reference: When you immediately add a widget to a container, it takes care of assuming the initial floating reference and you don't have to worry about reference counting at all ... just call to get rid of the widget. | ||||||
1.6. | How do I use GTK+ with threads? | |||||
This is covered in the GDK threads documentation . See also the GThread documentation for portable threading primitives. | ||||||
1.7. | How do I internationalize a GTK+ program? | |||||
Most people use GNU gettext , already required in order to install GLib. On a UNIX or Linux system with gettext installed, type to read the documentation. The short checklist on how to use gettext is: call so gettext can find the files containing your translations, call to set the default translation domain, call to request that all translated strings are returned in UTF-8, then call to look up each string to be translated in the default domain. Conventionally, people define macros as follows for convenience: You use (N stands for no-op) to mark a string for translation in a context where a function call to is not allowed, such as in an array initializer. You eventually have to call on the string to actually fetch the translation. both marks the string for translation and actually translates it. Nowadays, GLib provides the common shorthand macros in the header file , so you don't have to define them yourself, just include that header. Code using these macros ends up looking like this: Libraries using gettext should use instead of , which allows them to specify the translation domain each time they ask for a translation. Libraries should also avoid calling , since they will be specifying the domain instead of using the default. For the macro can be defined as: Again, GLib comes with the , saving you the trouble of defining the macros by hand. The macros in that header expect the translation domain to be specified by the macro. | ||||||
1.8. | How do I use non-ASCII characters in GTK+ programs ? | |||||
GTK+ uses Unicode (more exactly UTF-8) for all text. UTF-8 encodes each Unicode codepoint as a sequence of one to six bytes and has a number of nice properties which make it a good choice for working with Unicode text in C programs:
and . Text coming from external sources (e.g. files or user input), has to be converted to UTF-8 before being handed over to GTK+. The following example writes the content of a IS0-8859-1 encoded text file to : For string literals in the source code, there are several alternatives for handling non-ASCII content:
If you are using to localize your application, you need to call to ensure that translated strings are returned in UTF-8 encoding. | ||||||
1.9. | How do I use GTK+ with C++? | |||||
There are two ways to approach this. The GTK+ header files use the subset of C that's also valid C++, so you can simply use the normal GTK+ API in a C++ program. Alternatively, you can use a "C++ binding" such as gtkmm which provides a native C++ API. When using GTK+ directly, keep in mind that only functions can be connected to signals, not methods. So you will need to use global functions or "static" class functions for signal connections. Another common issue when using GTK+ directly is that C++ will not implicitly convert an integer to an enumeration. This comes up when using bitfields; in C you can write the following code: while in C++ you must write: There are very few functions that require this cast, however. | ||||||
1.10. | How do I use GTK+ with other non-C languages? | |||||
See the list of language bindings on http://www.gtk.org . | ||||||
1.11. | How do I load an image or animation from a file? | |||||
To load an image file straight into a display widget, use [1 ] . To load an image for another purpose, use . To i load an animation, use . can also load non-animated images, so use it in combination with to load a file of unknown type. To load an image or animation file asynchronously (without blocking), use GdkPixbufLoader . | ||||||
1.12. | How do I draw text ? | |||||
To draw a piece of text, use a Pango layout and , using code like the following: Do not use the deprecated GdkFont and . See also the "Text Handling in GTK 2" section of Porting applications to the GNOME 2.0 platform . | ||||||
1.13. | How do I measure the size of a piece of text ? | |||||
To obtain the size of a piece of text, use a Pango layout and , using code like the following: Do not use the deprecated function . See also the "Text Handling in GTK 2" section of Porting applications to the GNOME 2.0 platform . | ||||||
1.14. | Why are types not registered if I use their macro ? | |||||
The macros are defined as calls to , and the i functions are declared as which allows the compiler to optimize the call away if it appears that the value is not being used. A common workaround for this problem is to store the result in a volatile variable, which keeps the compiler from optimizing the call away. | ||||||
1.15. | How do I create a transparent toplevel window ? | |||||
To make a window transparent, it needs to use a visual which supports that. This is done by getting the RGBA colormap of the screen with and setting it on the window. Note that will return if transparent windows are not supported on the screen; also note that this may change from screen to screen, so it needs to be repeated whenever the window is moved to a different screen. One possibility to fill the alpha channel on the window is to use . Note that the presence of an RGBA visual is no guarantee that the window will actually appear transparent on screen. On X11, this requires a compositing manager to be running. See for a way to find out if the alpha channel will be respected. | ||||||
2. Which widget should I use... | ||||||
2.1. | ...for lists and trees? | |||||
See tree widget overview — you should use the GtkTreeView widget. (A list is just a tree with no branches, so the tree widget is used for lists as well.) Do not use the deprecated widgets GtkTree or GtkCList /GtkCTree in newly-written code, they are less flexible and result in an inferior user interface. | ||||||
2.2. | ...for multi-line text display or editing? | |||||
See text widget overview — you should use the GtkTextView widget. Do not use the deprecated widget GtkText in newly-written code, it has a number of problems that are best avoided. If you only have a small amount of text, GtkLabel may also be appropriate of course. It can be made selectable with . For a single-line text entry, see GtkEntry . | ||||||
2.3. | ...to display an image or animation? | |||||
GtkImage can display images in just about any format GTK+ understands. You can also use GtkDrawingArea if you need to do something more complex, such as draw text or graphics over the top of the image. | ||||||
2.4. | ...for presenting a set of mutually-exclusive choices, where Windows would use a combo box? | |||||
With GTK+, a GtkComboBox is the recommended widget to use for this use case. This widget looks like either a combo box or the current option menu, depending on the current theme. If you need an editable text entry, use GtkComboBoxEntry . | ||||||
3. GtkWidget | ||||||
3.1. | How do I change the color of a widget? | |||||
See , , , and . See GTK+ resource files for more discussion. You can also change widget color by installing a resource file and parsing it with . The advantage of a resource file is that users can then override the color you've chosen. To change the background color for widgets such as GtkLabel that have no background, place them in a GtkEventBox and set the background of the event box. | ||||||
3.2. | How do I change the font of a widget? | |||||
This has several possible answers, depending on what exactly you want to achieve. One option is . Note that this function can be used to change only the font size, as in the following example: If you want to make the text of a label larger, you can use : This is preferred for many apps because it's a relative size to the user's chosen font size. See if you are constructing such strings on the fly. You can also change the font of a widget by putting gtk-font-name = "Sans 30" in a resource file and parsing it with . The advantage of a resource file is that users can then override the font you have chosen. See GTK+ resource files for more discussion. | ||||||
3.3. | How do I disable/ghost/desensitize a widget? | |||||
In GTK+ a disabled widget is termed "insensitive." See . | ||||||
4. GtkTextView | ||||||
4.1. | How do I get the contents of the entire text widget as a string? | |||||
See and or . | ||||||
4.2. | How do I make a text widget display its complete contents in a specific font? | |||||
If you use with appropriate tags to select the font, the inserted text will have the desired appearance, but text typed in by the user before or after the tagged block will appear in the default style. To ensure that all text has the desired appearance, use to change the default font for the widget. | ||||||
4.3. | How do I make a text view scroll to the end of the buffer automatically ? | |||||
A good way to keep a text buffer scrolled to the end is to place a mark at the end of the buffer, and give it right gravity. The gravity has the effect that text inserted at the mark gets inserted before , keeping the mark at the end. To ensure that the end of the buffer remains visible, use to scroll to the mark after inserting new text. The gtk-demo application contains an example of this technique. | ||||||
5. GtkTreeView | ||||||
5.1. | How do I associate some data with a row in the tree? | |||||
Remember that the GtkTreeModel columns don't necessarily have to be displayed. So you can put non-user-visible data in your model just like any other data, and retrieve it with . See the tree widget overview . | ||||||
5.2. | What's the GtkTreeView equivalent of ? | |||||
As there is no separate data column in the GtkTreeModel , there's no built in function to find the iter from data. You can write a custom searching function to walk the tree and find the data, or use . | ||||||
5.3. | How do I put an image and some text in the same column? | |||||
You can pack more than one GtkCellRenderer into a single GtkTreeViewColumn using or . So pack both a GtkCellRendererPixbuf and a GtkCellRendererText into the column. | ||||||
5.4. | I can set data easily on my GtkTreeStore /GtkListStore models using and , but can't read it back? | |||||
Both the GtkTreeStore and the GtkListStore implement the GtkTreeModel interface. Consequentially, the can use any function this interface implements. The easiest way to read a set of data back is to use . | ||||||
5.5. | How do I change the way that numbers are formatted by GtkTreeView ? | |||||
Use or and do the conversion from i number to string yourself (with, say, ). The following example demonstrates this: | ||||||
5.6. | How do I hide the expander arrows in my tree view ? | |||||
Set the expander-column property of the tree view to a hidden column. See and . | ||||||
6. Using cairo with GTK+ | ||||||
6.1. | How do I use cairo to draw in GTK+ applications ? | |||||
Use to obtain a cairo context for drawing on a GDK window or pixmap. See Cairo Interaction for some more useful functions. | ||||||
6.2. | I have created a cairo context with , but when I later use it, my drawing does not show up. Why is that ? | |||||
All drawing in GTK+ is normally done in an expose handler, and GTK+ creates a temporary pixmap for double-buffering the drawing. If you create a cairo context outside the expose handler, it is backed by the GDK window itself, not the double-buffering pixmap. Consequently, any drawing you do with that cairo context gets overwritten at the end of the expose handler, when the double-buffering pixmap is copied back. Possible solutions to this problem are:
| ||||||
6.3. | Can I improve the performance of my application by using the Glitz backend of cairo ? | |||||
No. The GDK X11 backend uses the cairo X backend (and the other GDK backends use their respective native cairo backends). The GTK+ developers believe that the best way to improving the GDK drawing performance is to optimize the cairo X backend and the relevant code paths in the X server that is uses (mostly the Render extension). | ||||||
6.4. | Can I use cairo to draw on a GdkPixbuf ? | |||||
No, at least not yet. The cairo image surface does not support the pixel format used by GdkPixbuf. |