29

By liuyanghejerry

No Comments

Categories: Qt Resource

Tags: , , ,

Gaussian Blur effect for QImage

Gaussian Blur is a common effect in normal gui development. Qt provide a class, QGraphicsBlurEffect, for QWidget to this effect.
However, what about QImage?
This is the code snippet I found in \qt\src\gui\image\qpixmapfilter.cpp:

QImage blurred(const QImage& image, const QRect& rect, int radius, bool alphaOnly = false)
{
    int tab[] = { 14, 10, 8, 6, 5, 5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 };
    int alpha = (radius < 1)  ? 16 : (radius > 17) ? 1 : tab[radius-1];

    QImage result = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
    int r1 = rect.top();
    int r2 = rect.bottom();
    int c1 = rect.left();
    int c2 = rect.right();

    int bpl = result.bytesPerLine();
    int rgba[4];
    unsigned char* p;

    int i1 = 0;
    int i2 = 3;

    if (alphaOnly)
        i1 = i2 = (QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3);

    for (int col = c1; col <= c2; col++) {
        p = result.scanLine(r1) + col * 4;
        for (int i = i1; i <= i2; i++)
            rgba[i] = p[i] << 4;

        p += bpl;
        for (int j = r1; j < r2; j++, p += bpl)
            for (int i = i1; i <= i2; i++)
                p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
    }

    for (int row = r1; row <= r2; row++) {
        p = result.scanLine(row) + c1 * 4;
        for (int i = i1; i <= i2; i++)
            rgba[i] = p[i] << 4;

        p += 4;
        for (int j = c1; j < c2; j++, p += 4)
            for (int i = i1; i <= i2; i++)
                p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
    }

    for (int col = c1; col <= c2; col++) {
        p = result.scanLine(r2) + col * 4;
        for (int i = i1; i <= i2; i++)
            rgba[i] = p[i] << 4;

        p -= bpl;
        for (int j = r1; j < r2; j++, p -= bpl)
            for (int i = i1; i <= i2; i++)
                p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
    }

    for (int row = r1; row <= r2; row++) {
        p = result.scanLine(row) + c2 * 4;
        for (int i = i1; i <= i2; i++)
            rgba[i] = p[i] << 4;

        p -= 4;
        for (int j = c1; j < c2; j++, p -= 4)
            for (int i = i1; i <= i2; i++)
                p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
    }

    return result;
}

And a demo:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QLabel label;
    QImage image("image.png");
    image =  blurred(image,image.rect(),10,false);
    label.setPixmap(QPixmap::fromImage(image));
    label.show();

    return a.exec();
}

Nice and simple, huh?

7

By liuyanghejerry

No Comments

Categories: Qt, Qt Resource

Tags: , ,

How to draw glow text using QPainter

Searching around, there’s no good way for a font filter when using QFont. Since QPainter still usable for us, we could use QPainter to draw text looks like glow.

Glow text can be treated as “text beyond text”, which means:

More layers, more glow we look like. The code can be seems like below:

void GlowTicker::paintEvent(QPaintEvent *)
{
QPainter painter(this);

painter.setOpacity(0.5);
painter.setPen(_glowColor);
painter.drawText(QRect(QPoint(1 – _offset, 2), textSize()), Qt::TextSingleLine, _text);
painter.drawText(QRect(QPoint(3 – _offset, 2), textSize()), Qt::TextSingleLine, _text);
painter.drawText(QRect(QPoint(2 – _offset, 1), textSize()), Qt::TextSingleLine, _text);
painter.drawText(QRect(QPoint(2 – _offset, 3), textSize()), Qt::TextSingleLine, _text);

int w = preferedWidth();
painter.drawText(QRect(QPoint(1 + w – _offset, 2), textSize()), Qt::TextSingleLine, _text);
painter.drawText(QRect(QPoint(3 + w – _offset, 2), textSize()), Qt::TextSingleLine, _text);
painter.drawText(QRect(QPoint(2 + w – _offset, 1), textSize()), Qt::TextSingleLine, _text);
painter.drawText(QRect(QPoint(2 + w – _offset, 3), textSize()), Qt::TextSingleLine, _text);

painter.setOpacity(1);
painter.setPen(_textColor);
painter.drawText(QRect(QPoint(2 – _offset, 2), textSize()), Qt::TextSingleLine, _text);
painter.drawText(QRect(QPoint(2 + w – _offset, 2), textSize()), Qt::TextSingleLine, _text);
}

For the layers, we need them transparent or it will influence the main layer.
Well, last show:

<a href="http://qtplus.info/wp-content/uploads/2011/08/glow-text1.png"><img class="alignnone size-full wp-image-74" title="glow text" src="http://qtplus.info/wp-content/uploads/2011/08/glow-text1.png" alt="" width="234" height="64" /></a>

25

By liuyanghejerry

No Comments

Categories: Qt Resource

Tags: ,

Two Inspectors for Qt

I just found two great tools for Qt. They’re Qt Inspector and basyskom Inspector.

These two inspectors are just like the Web Inspector of Webkit or the Firebug of FireFox. You can use them view or edit a Qt-based program without recompile, like shown below:

inspector-dolphin-settings

Qt Inspector worked with Dolphin

Without more test, I’m not sure if these two tools can work on Windows or only on KDE. If you’re interested in them and would like give them a whirl, tell me your results.

16

By liuyanghejerry

No Comments

Categories: Qt, Qt Resource

Tags: , ,

Couples of widgets — wwWidgets

wwWidgets is a set of widgets, which can be GPL or Commercial, made by Witold Wysota:

wwWidgets is a professional set of useful widgets for Qt 4. It consists of several different widgets that are either enhanced versions of widgets bundled with Qt or completely new ones that implement functionality not available in Qt including custom multipage container widgets that can hold other widgets.

The classes follow all guidelines for building new widgets. Thanks to that they can be used with different widget styles (like Plastique or WindowsXP) and they are easily stylable using Qt style sheets allowing a perfect blend with the rest of your application.

All widgets can be used directly from within Qt Designer thanks to the widget plugin acompaniating the library. The plugin makes it possible to place wwWidgets on forms and edit their properties and contents just like for default widgets – using the property browser and dedicated property editors for complex properties like sets of colors.

wwWidgets are well documented, after installing the package, the reference is available from within Qt Assistant. The documentation looks like the one available for default Qt classes so it integrates well with it – you can browse and search the documentation and all wwWidgets component symbols can be found in the index.

Its website:http://www.wysota.eu.org/wwwidgets/

27

By liuyanghejerry

No Comments

Categories: Qt, Qt Resource

Tags: ,

QtOgreFramework

While looking around, I found a framework combines Ogre with Qt. Ogre is a well-known open-source 3D graphic engine, that brings impressive effects. The framework supports…

  • A configuration dialog:
    • Contains a ‘Graphics configuration widget’ to configure Ogre.
    • Support for rendering systems, resolution, etc.
    • All config settings stored to an .ini file
  • A log viewer:
    • Syntax highlighting and filtering
    • Qt’s message handler are redirected to this system
    • Ogre’s logs are also redirected
  • A Frames Per Second widget
    • Actaully a Qt Dialog
    • Window decorations turned off
    • Custom mouse handling so it can be dragged around
  • Simple to use interface:
    • Just inherit from ‘GameLogic’ class and override the desired functions
    • Demo is included which shows basic usage
    • Very few assumptions made so you have maximum flexibility
  • Support for transparent windows on some systems (Vista, Linux with Compviz)

As above, it may help if you want make a game with both Qt and Ogre. Unluckily, it seems not been developed since Aug, 2010 :(

Links:

http://www.ogre3d.org/forums/viewtopic.php?f=11&t=45709
https://ogreaddons.svn.sourceforge.net/svnroot/ogreaddons/trunk/QtOgreFramework/