Search code examples
c++qtqimageqicon

How to put QImage with gray background inside QIcon?


I'm doing:

QIcon(QPixmap::fromImage(img));

and then putting it to QToolButton with setIcon and setIconSize

I need to have my img surrounded with gray color inside this icon - how can I implement this?

alike there: enter image description here


Solution

  • Setting a Stylesheet should do the job for you..

    int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            QToolButton b;
            b.setStyleSheet("QToolButton { background-color: grey }");  
            QImage img("C:\\Users\\Administrator\\Desktop\\Icon.png");
            b.setIcon( QPixmap::fromImage(img) );
            b.show();
            a.exec();
        }
    

    Try this out. You can also play around with the QPalette of the button, that also provides you a very fine control over the way you want your widgets to look.