十二
16
Plus Your Qt
十二
16
Now the newest version of Qt, Qt 4.8, is released at http://labs.qt.nokia.com/2011/12/15/qt-4-8-0-released/.
The new version has been under beta for about 5 months, intros many new changes, including the new Webkit inside and a lighthouse. release note is at here, and you can download Qt 4.8 here.
十
29
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
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
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>