views:

489

answers:

1

Hi,

I'm trying to set transparent icons in a QAction, which is then added to a Menu and a toolbar. I'm styling the application with a style sheet. The icon transparency works, but the icons are being drawn on the toolbar with what looks like a 1px black border on the left and top edges of the icons.

Now, all my icons are stored in one large image file (PNG, with transparency) - they're saved in one large strip. To extract them into a single QIcon, I do this:

// load icon strip:
QPixmap large;
large.load(":/icons/tb_icons_l.png", "PNG", Qt::OrderedAlphaDither);
QSize largeSize(large.width() / ICON_COUNT, large.height());

// create individual icon pixmap
QPixmap iconLarge(largeSize);
// fill with transparent pixels:
iconLarge.fill(QColor(0,0,0,0));
// copy pixel data from icon strip to image:
{
    QPainter p(&iconLarge);
    p.setBackgroundMode(Qt::TransparentMode);
    p.drawPixmap(0,0,large, largeSize.width() * i, 0, largeSize.width(), largeSize.height()); // 'i' is the icon index.
}

return QIcon(iconLarge);

I know the problem is in the few lines above, since when I load icons from individual files instead this all works perfectly ( no black border).

Has anyone else seen anything like this before? Can anyone suggest some changes that will remove the unsightly black border? The border is definitely part of the image, rather than part of the toolbar button itself.

+1  A: 

First of all, I think you're doing things in an unnecessarily complicated way by having them all in one image. However...

What version of Qt are you running? On what platform? At one point about 6-12 months ago (I think), I encountered a bug with drawing one transparent image on top of another transparent image as QPixmaps. Some of the pixels turned other colors, somewhat randomly as far as I could tell. This was on Linux, with either Qt4.4 or 4.5 (I can't remember). Whichever one it was, I submitted a bug report, and it was acknowledged as a regression and fixed in the next bug-fix release. That means if you aren't running with the latest version, you might do well to update to the latest. (The work-around was to draw onto a QImage, then convert it to a QPixmap when needed.)

Alternately, you could try a test where you get rid of the indexing, and just load an image that you know the size of into the painter, to see if you can simplify your code and still reproduce the problem.

Caleb Huitt - cjhuitt
I'm running the latest Qt 4.5 under Linux.
Thomi
Thanks - you were right. There was a new bugfix release I had not seen. Updating fixed the issue. Cheers! At least my code was not to blame!
Thomi