GTK+ and Qt Creator

This aim of this post is to explain how to use the Graphical Tool Kit (GTK+) with the Qt Creator IDE. This configuration has been done with the following software versions :

  • Ubuntu 14.04 LTS (and 12.04 LTS)
  • Qt Creator 3.0.1
  • GTK 3

Package installation

First, install Qt creator if it is not done yet:

sudo apt-get install qtcreator

Now install the GTK+ developpment package:

sudo apt-get install libgtk-3-dev 

Project configuration

Create a new Qt Application Console:

Capture du 2014-12-04 11:35:53

Once, the project is created, edit the project file (.pro) and disable Qt libraries (QT-=gui and QT-=core). Add the following lines to use the GTK+ libraries

unix: CONFIG += link_pkgconfig
unix: PKGCONFIG += gtk+-3.0

Your project file should look like this:

# Disable Qt core and Qt Graphical user interface (don't use Qt)
QT	    -= core
QT	    -= gui

# Name of the target (executable file)
TARGET	    = gtk_test

# This is a console application
CONFIG	    += console
CONFIG	    -= app_bundle

# This is an application
TEMPLATE    = app

# Sources files
SOURCES	    += main.cpp

# GTK+ library
unix: CONFIG	+= link_pkgconfig
unix: PKGCONFIG += gtk+-3.0

Copy and past this simple example in the file main.cpp, build and run the application.

#include 

int main (int   argc,    char *argv[])
{
  // Initialize GTK environment
  gtk_init (&argc, &argv);
  // Create a new window
  GtkWidget *window;
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  // Set title to this new window
  gtk_window_set_title (GTK_WINDOW (window), "www.lucidarme.me");
  // Connect the signal to close the window
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  // Show the window
  gtk_widget_show (window);
  // Run main GTK loop
  gtk_main ();
  return 0;
}

If everything is fine, an empty window should appear:
Screenshot from 2014-12-04 17:50:55

Download


Leave a Reply

Your email address will not be published. Required fields are marked *