Andreas
Qt
Labs
Graphics View
Graphics Items
Posted by Andreas
 in Qt, Labs, Graphics View, Graphics Items
 on Wednesday, December 20, 2006 @ 07:33

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);
    image.fill(0);

Prepare a suitably-sized transparent image.

    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing);
    scene.render(&painter);
    painter.end();

Render the scene onto the image.

    QBuffer buffer;
    buffer.open(QIODevice::WriteOnly);
    QImageWriter writer(&buffer, "png");
    writer.write(image);

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()));
    printf("rn");

…the CGI header. Finally,

    QFile file;
    file.open(stdout, QIODevice::WriteOnly);
    file.write(buffer.data(), buffer.data().size());
    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.

3 Responses to “What’s this? CGI?”

» Reply from Andreas
 on Wednesday, December 20, 2006 @ 20:16
Andreas

Include , or <QtGui>, looks like WordPress doesn’t understand my html. :-)



© 2008 Nokia Corporation and/or its subsidiaries. Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide.
All other trademarks are property of their respective owners.