Using Cairo/Pango with Qt (on Linux/X11)
Can I render widgets with Cairo in Qt? I'm probably not the first one asking this question. Cairo gained much momentum in the last few years. It now delivers well maintained graphics backends for xlib/xrender, Quartz and even the win32 legacy. I will show for short on how to use Cairo on a QPixmap on Linux/Xorg. In principle it should be the same easy on the other platforms.
Linker flags for your qmake *.pro file:
unix:INCLUDEPATH += /usr/include/glib-2.0 /usr/lib/glib-2.0/include /usr/include/cairo /usr/include/pango-1.0 unix:LIBS += -L/usr/lib -lcairomm-1.0 -lcairo -lpangocairo-1.0
Qt/C++ code to obtaining a Cairo surface to draw onto:
buf_ = QPixmap(width(), height()); buf_.fill(Qt::white); const QX11Info& info = buf_.x11Info(); Display* display = info.display(); Drawable drawable = buf_.handle(); Screen* screen = XScreenOfDisplay(display, info.screen()); Visual* visual = reinterpret_cast<visual>(info.visual()); XRenderPictFormat *format = XRenderFindVisualFormat(display, visual); surface_ = cairo_xlib_surface_create_with_xrender_format(display, drawable, screen, format, width(), height()); cr_ = cairo_create(surface_);
Where surface_ is of type cairo_surface_t* and cr_ of type cairo_t*.
To short of an explanation? Check out the entire example project from here. Run the usual "qmake && make" on it to get it compile. It simply renders the contents of the file provided as first command line argument onto a Cairo surface.
Tags:

for online purchase. For mail order please use