29

By liuyanghejerry

1 Comment

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?

22

By liuyanghejerry

No Comments

Categories: Qt Official

Tags:

Qt Project is live now

The Qt Project, located at http://qt-project.org/ is live today early.

After Nokia abandoned Meego system, Qt is almost no use for it. Happily, the project is open now and Qt is like other opensource software.

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.

24

By liuyanghejerry

No Comments

Categories: Qt Official

Tags: ,

Qt 4.8 Beta is coming

Few days ago, Qt labs post a new article that says Qt 4.8 beta is released. This is the first beta of Qt 4.8 and includes the main features below:

  • Lighthouse, a clean abstraction layer for porting Qt to new GUI platforms
  • Threaded OpenGL support
  • Multithreaded HTTP
  • Optimized file system access
  • Native Symbian networking, Ipv6 and OpenGL Graphics, Symbian resource management
  • New localisation and IP multicast APIs
  • Right-to-left text support, on-screen keyboard support and improved image caching in Qt Quick
Noticed that the lighthouse is included so that it may help porting Qt onto new devices like smart phones.
Now, the source code can be download either with .zip or with .tar.gz, and the checksum.txt is also available here.

15

By liuyanghejerry

No Comments

Categories: Misc

Tags: , , ,

QGradualBox is under developing

A new widget QGradualBox is started just now.

This new widget can show messages faded in and faded out. It can be use as a message box that won’t interrupt user.

Now you can download it from our git repository.

However, it’s just a new widget being developed, not ready for actual use. For example, one of the current bug is that setBgColor() , setFontColor() and setBorderColor() will function immediately not wait until the next showText().

Once again, our git repository is https://github.com/liuyanghejerry/Qt-Plus. Welcome to join with me.

4

By liuyanghejerry

No Comments

Categories: Qt Widget

Tags: , ,

QIRCLineEdit

This is a IRC chat like LineEdit widget.

It uses a QList<QString> to record the history input, and works when slot myclear() was emitted.

Source Download:

QIRCLinedit