[Interest] QIcon on/off doesn't work on QAction

Sensei senseiwa at gmail.com
Wed Jul 3 11:36:26 CEST 2013


On 7/3/13 3:42am, Alex Malyushytskyy wrote:
> First. Your code should create a memory leak.
> Cause addAction will make a copy of icon and you never delete result of
> colorize
> Look at declaration:
> QAction *	*addAction <qtoolbar.html#addAction-5>*( const QIcon &/icon/,
> const QString &/text/, const QObject */receiver/, const char */member/)

Thanks Alex! I am using a temporary object now.


> Second. You did not clarify what "does not work" means.
> Anyway my guess you should either set
> use QToolbar::setIconSize to the size of your pixmap or add compatible
> (by size ) pixmap to your icon.
> The icons you are using are definitely not compatible with default
> toolbar icon sizes, so at the best the enabled icon is auto scaled, then
> greyed and that what you may see, at the wrse you see no icon at all (
> sorry too lazy to test).

Ok, I've added the following code that includes in the QIcon the 
original version and the scaled down one, but it doesn't work: when I 
click on the toolbar's action the icon doesn't get blue.


     // In the main window
     toolbar = new QToolBar(tr("Tools"), this);
     toolbar->setIconSize(QSize(16, 16));


And:


QIcon colorize(const QString& pix)
{
     QPixmap orig(pix), blue(orig.size());
     blue.fill(QColor(64 - 32, 160 - 32, 255 - 32));
     blue.setMask(orig.createMaskFromColor(Qt::transparent));

     QIcon i;
     i.addPixmap(blue, QIcon::Normal, QIcon::On);
     i.addPixmap(orig, QIcon::Normal, QIcon::Off);
     i.addPixmap(blue.scaled(16, 16), QIcon::Normal, QIcon::On);
     i.addPixmap(orig.scaled(16, 16), QIcon::Normal, QIcon::Off);

     return i;
}


Even scaling with bilinear filtering doesn't do what I expected. For 
competeness, the original PNG is 128x128 with alpha channel.


I am missing something here...








More information about the Interest mailing list