What’s this? CGI?

After Simon and Paul made some advancements to Qt's font rendering system, it's possible to render fonts in Qt running in Tty / console mode; that is, you don't need an X11 connection. That means that the last piece of the puzzle is in place to use Qt for CGI scripting. :-) It sort of always worked, but now also for text. Guess what this is:

It's a screenshot of output generated from a Qt CGI script based on Graphics View. Here's how to do it:

#include <QtGui>

int main(int argc, char **argv)
QApplication app(argc, argv, QApplication::Tty);

Create your QApp with QApplication::Tty (former 'false') to run in console mode.

    QGraphicsScene scene;
scene.addEllipse(QRectF(0, 0, 100, 100), QPen(Qt::black, 2), QBrush(QColor(64, 78, 128, 192)));

Create a scene, just anything. Text, shapes, or your existing scene. Try adding text and see it work.

    QImage image(scene.itemsBoundingRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
image.setColorTable(QVector() << Qt::transparent);

Prepare a suitably-sized transparent image.

    QPainter painter(&image);

Render the scene onto the image.

    QBuffer buffer;;
QImageWriter writer(&buffer, "png");

Use QImageWriter to stuff the image into a QBuffer so that we can get its size for...

    printf("Content-Type: image/png; name="image.png"rn");
printf("Content-Length: %drn", int(buffer.size()));

...the CGI header. Finally,

    QFile file;, QIODevice::WriteOnly);
return 0;

Dump the image to stdout, compile, and copy your new script to your public_html/cgi-bin directory. ;-)

Disclaimer: This happens to work, tested in 4.3 snapshots only, may work with 4.2, and the snapshots may change at any time. It's not officially supported.

Blog Topics: