How-to: Use Graphviz to Draw Graphs in a Qt Graphics Scene

Well, it’s been a long time. This post will be dedicated to explanations on how to draw graphs in Qt’s QGraphicsScene, using Graphviz. We’re not talking about rendering an SVG graph with Graphviz and then printing it in a scene, however. What we will do, instead, is:

represent a graph, using a C++ wrapper class for libgraph

tell Graphviz to compute positions for each node, and the path of each edge

draw our graph using QGraphicsEllipseItem and QGraphicsPathItem

The whole thing is used in a computer security research project on which I’m not allowed to give any information, so I will be vague on some parts of this tutorial, and I will not provide a whole bunch of ready-to-run code. I’m still going to give you the hints for fulfilling the three tasks above, and I will publish the class I wrote for my project (it is, of course, not generic at all, since it was designed for my particular needs).

This tutorial assumes you have a decent knowledge of how Graphviz works, and basic knowledge of the QGraphics API. Here is a preview of a possible result with this tutorial, with no tweaking:

Using libgraph to represent a Graphviz graph

A little note on the libgraph API

Before we begin, I would like to point out that the libgraph API is not always well designed. One thing that annoyed me is that all calls to the library require char * parameters when only const char * are needed, which results in compilation warnings. Since I don’t like warnings, I wrote wrappers for the functions that I needed, so I wouldn’t have to use qPrintable(), and I could hide the warnings with const_cast. For instance (the methods will be described in the next sections):

Other functions defined this way: _agnodeattr(), _agedgeattr(), _agnode() and _gvLayout().

Basic wrapper class

The first step is to represent a graph so that Graphviz can compute it. Actually, Graphviz provides libraries for that, so you don’t need to write a dot-syntax file and then use the very ugly (and completely unacceptable inside an application, in my opinion) system() call. A good introduction to this library is available as a PDF guide on Graphviz’s website. I recommend reading it before going any further, because I may forget to explain things about libgraph!

Unfortunately, there is no official C++ port of libgraph, and we’re doing C++ here, so we may want an object that will represent a Graph, with methods for adding Nodes and Edges. Here is a beginning of definition for such a class, which also contains bits of code for managing the aspect of our graph:

We will now see how to add and remove nodes. The node removal function takes care of removing edges as well, so you may want to come back to it after we introduced edges (eventhough the code is pretty straightforward).

The special function setRootNode() allows telling the dot layout algorithm which node we want to use as a root for generating the graph (as dot usually displays tree-like graphs, it can start drawing them from a particular node).

The setFont() method allows you to tell Graphviz which font you want to use for the rendering. You can pass the constructor your application’s current font and write an event handler for when the font changes in your application, that will call this function. Note that I’m not sure it updates the font for existing nodes and edges (in which case you would have to call _agset() on all of them).

Compute node positions and edge paths

Surprisingly, this is the very easy part of the job. Graphviz already has several implementations of common graph drawing algorithms, used for different kinds of graphs. In Graphviz, they are called layout, and the most known one is dot. It also is the one I’m using, but you may write additional methods in your class for calling different kinds of layouts – just be aware that some layouts require extra attributes, so you may also want to modify other parts of the class depending on what you need.

At this point, you can even render your graph to a file, or to the standard output, using gvRender() and gvRenderFilename() (see the doc). You can also render your graph a first time, and then apply another layout on your rendered graph (some layouts can use position hints already provided in the graph, see the PDF guide.

Once the layout has been called, many more attributes are set in the graph, nodes and edges’s data field. Trying to access this information in the objects before would have lead to unitialised values and null pointers, so there are things that we can do only after having applied a layout. Among these things, we can now retrieve the exact size of the graph in points, and give this information to our QGraphicsView’s setSceneRect() method, which defines the area of the QGraphicsScene that the user can see.

Two notes about this function: it returns a lovely QRectF object, that we can manipulate much more easily than raw coordinates in Qt ; More important, it converts points into pixels, and it does so by applying a ratio equal to your actual DPI (96) divided by Graphviz’s DPI (72). Of course, you may want to replace 96 by a class parameter or a constant. Just don’t forget that libgraph excepts a coma and not a period as a separator for the integer and the floating part of reals.

Transform the layout attributes into easy-to-use objects

The next step consists of exploiting these attributes in order to draw our graph. As we don’t want to manipulate structs and pointers, however, we will go through a translation step, and transform this raw data into sweet objects, so that all the complex code remains inside our class. Below are the said objects. As you can see, I did not bother with setters and getters, but these objects have a short lifespan, and they’re only passed by copy to users of the GVGraph, so it didn’t really matter to me what was done with them afterwards.

/// A struct containing the information for a GVGraph's node
struct GVNode
{
/// The unique identifier of the node in the graph
QString name;
/// The position of the center point of the node from the top-left corner
QPoint centerPos;
/// The size of the node in pixels
double height, width;
};
/// A struct containing the information for a GVGraph's edge
struct GVEdge
{
/// The source and target nodes of the edge
QString source;
QString target;
/// Path of the edge's line
QPainterPath path;
};

Two additional methods must now be added in order to provide these items to users. The simplest one is the node retrieval method, and most of it is straightforward. Just note that Graphviz calculates coordinates starting by the bottom-left corner, while QGraphics uses the top-left corner, so all
y-coordinates must be translated.

The serious trouble begins just now: we need to transform a plethora of coordinates into a QPainterPath that can then be used in order to draw a line. Those of you who already messed with Qt’s Bezier curves API know how it works: cubicTo() takes two arguments for the points that the curve must pass by, and one for the ending point, so you have to do multiple calls to it for drawing an entire spline. Very fortunately, it seems that the number of coordinates given by Graphviz always is 3n+1, which makes it possible to easily loop over the list of coordinates. The loop method I’m using is based on the one used for transforming Qt3 spline canvas objects into Qt4 Path objects.

Voilà, we now have proper C++ items with all the relevant information in the way Qt expects it to be. All that’s left is drawing the actual QtGraphicsItems.

Draw the graph with Qt

I am not going to provide code for this last part of the tutorial, as it would involve publishing code that belongs to the project I work on.

All you have to know is that nodes are to be represented by QGraphicsEllipseItem objects (unless you specified another shape for Graphviz nodes, using the shape node attribute), and that these nodes must be positioned using the centerPos attribute of GVNode. This can be easily achieved by writing a setCenterPos() method that will translate the center coordinates into top-left coordinates and then call the setPos() method itself.

As for edges, they typically are QGraphicsPathItem, and you only have to pass them the GVEdge’s path when constructing them, or via the setPath() method. Easy as pie! And here we go for the comparison between Graphviz rendering and my own Qt-rendered objects (with support for all the QGraphicsItem events):